简体   繁体   English

无法弄清楚此JS代码的含义

[英]Can't figure out what this JS code means

I've been romping through a piece of JS I came across online and can't figure out what this piece of code means. 我一直在浏览网上遇到的一段JS,无法弄清楚这段代码的含义。

global$string$newLines = function(a) {
    return a.replace(/(\r\n|\r|\n)/g, "\n");
},

I'm specifically wondering about the piece /(\\r\\n|\\r|\\n)/g 我特别想知道/(\\r\\n|\\r|\\n)/g

Also - Is this machine generated code? 另外-这台机器是否生成代码? Is that why the variable name is full of $s? 这就是为什么变量名充满$ s吗?

They are regular expresions 它们是常规表达式

\r = Find a carriage return character
\n = Find a new line character

the /g (g only) mean to find all / g(仅g)表示查找所有

http://www.w3schools.com/jsref/jsref_obj_regexp.asp http://www.w3schools.com/jsref/jsref_obj_regexp.asp

So the code mean to find all \\r\\n or just \\r or just \\n and replace it with \\n They are whitespace characters so they not visible. 因此,该代码意味着找到所有\\ r \\ n或仅找到\\ r或仅找到\\ n并将其替换为\\ n。它们是空格字符,因此它们不可见。

It's a regular expression for replacing newline characters. 这是用于替换换行符的正则表达式。

There are different types of new line characters inserted by various browsers/editors/OSes etc. 各种浏览器/编辑器/操作系统等都插入了不同类型的换行符。

\\n is the default on all (true) Unix systems with \\r having no meaning, C, Java, C++, etc, adopted this convention. \\n是所有(true)Unix系统上的默认设置,而\\ r没有任何意义,C,Java,C ++等采用此约定。

\\r is from the days of Mac before it was a Unix system, while the duplicate \\r\\n is the Windows way. \\r是Mac之前的Unix系统,而复制的\\r\\n是Windows。

The /g flag represents a global setting telling the regular expression to search the entire document. /g标志表示全局设置,该设置告诉正则表达式搜索整个文档。

So what the code is doing is using a regular expression to globally find all possible equivalents of a newLine, and replacing them with the defacto standard, '\\n' 因此,代码正在执行的操作是使用正则表达式全局查找newLine的所有等效项,然后将其替换为实际标准'\\ n'

This is just a regular expression used to replace Carriage Returns and New Line characters with new line characters. 这只是一个正则表达式,用于用换行符替换回车符和换行符。

Your Regex: /(\\r\\n|\\r|\\n)/g 您的正则表达式: /(\\r\\n|\\r|\\n)/g

Explanation: 说明:

1st Capturing group (\\r\\n|\\r|\\n) 第一捕获组(\\ r \\ n | \\ r | \\ n)

1st Alternative: \\r\\n 第一种选择: \\ r \\ n

\\r matches a carriage return (ASCII 13) \\n matches a line-feed (newline) character (ASCII 10) \\ r匹配回车符(ASCII 13)\\ n匹配换行符(换行符)(ASCII 10)

2nd Alternative: \\r 第二种选择: \\ r

\\r matches a carriage return (ASCII 13) \\ r匹配回车符(ASCII 13)

3rd Alternative: \\n 第三选择: \\ n

\\n matches a line-feed (newline) character (ASCII 10) \\ n与换行符(ASCII 10)匹配

g modifier : global. g修饰符 :全局。 Give All matches (ie Don't return on first match). 给出所有匹配项(即在第一场比赛中不返回)。

PS: Check out regex101.com for generating such beautiful explanation for any Regex. PS:请查看regex101.com ,以为任何Regex生成如此漂亮的解释。

The code replaces carriage-return/new-line combinations with a single newline. 该代码用单个换行符代替回车/换行符组合。

The $'s in the variable name is done by several javascript compilers out there. 变量名中的$由几个JavaScript编译器完成。 Developers will often break their code up into namespaces of the form global.string.newline , for example. 例如,开发人员通常会将他们的代码分解为global.string.newline形式的名称空间。 But when we want to run that code on a client, it's safer and more efficient to turn this object-within-an-object-within-an-object into a single variable. 但是,当我们想在客户端上运行该代码时,将一个对象内的对象转换为单个变量会更安全,更有效。 Usually, the javascript compiler will go one step further and then turn this long variable name into some short unique sequence, but it will also preserve this intermediate form for easier debugging. 通常,javascript编译器会更进一步,然后将此长变量名转换为一些短的唯一序列,但它还将保留此中间形式,以便于调试。

It is a regex to remove the carriage return/new line/carriage return + new line with new line from a string. 这是一个正则表达式,用于从字符串中删除回车符/新行/回车符+新行。

/(\r\n|\r|\n)/g 

the /g in the end signifies globally, hence throughout the string and not just the first occurence. 最后的/ g表示全局,因此在整个字符串中不仅只是第一次出现。

Working Fiddle 工作小提琴

JS Code: JS代码:

global$string$newLines = function (a) {
    return a.replace(/(\r\n|\r|\n)/g, "\n")
}

function abc() {
    var text = document.getElementById("test").value;
    console.log(global$string$newLines(text));
}

HTML Code: HTML代码:

<textarea id="test"></textarea>
<button id="testClick" onclick="abc()">Click</button>

This is a regular expression replace that means: 这是一个正则表达式替换,表示:

Find any occurence of either: 查找以下任一情况:

  • \\r\\n \\ r \\ n
  • \\r \\ r
  • \\n \\ n

And replace it with \\n. 并将其替换为\\ n。

Comments: 评论:

  • The /g means it will mach all findings, not only the first occurence. / g表示它将处理所有发现,而不仅仅是第一次出现。
  • The third option to replace \\n by \\n is nonsense as it has no effect. 用\\ n替换\\ n的第三个选项是无意义的,因为它没有效果。

Doc of the replace and link to regex: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace 替换文档并链接到正则表达式: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/replace

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM