简体   繁体   English

任何人都可以解释这段Javascript代码吗?

[英]Can anyone explain this snippet of Javascript code?

Reading this discussion , I didn't understand what this means: 读这个讨论 ,我不明白这意味着什么:

 $1X$2

Simply X ? 只是X

And about this other code: 关于这个其他代码:

 str = str.replace(/(<script.*?>)(.*)(?=<\/script>)/gi, function(x,y,z)
 {return y+z.replace(/a/gi,'Z')})

Here is what I didn't understand: 这是我不明白的:

  • ?=
  • how does function(x,y,z) works? function(x,y,z)工作? ie from where it take x , y and z ? 即从哪里采取xyz

I'll be very grateful if someone could explain in a clear way this code. 如果有人能够以清晰的方式解释这段代码,我将非常感激。

  1. The string "$1X$2" used as the second argument to a .replace() call includes references to groups from the regular expression first argument. 用作.replace()调用的第二个参数的字符串“$ 1X $ 2”包括对正则表达式第一个参数的组的引用。 Groups — portions of the regular expression in parentheses — collect the characters they match for use later. 组 - 括号中正则表达式的一部分 - 收集它们匹配的字符以供日后使用。 The substrings "$1" and "$2" mean, "whatever was matched by the group 1" (or group 2). 子串“$ 1”和“$ 2”表示“组1匹配的任何东西”(或组2)。

  2. When the second argument to a call to .replace() is a function, JavaScript passes the match information to the function as arguments. 当调用.replace()的第二个参数是一个函数时,JavaScript将匹配信息作为参数传递给函数。 The first argument is always the entire match. 第一个参数始终是整个匹配。 The subsequent arguments are the matched groups. 后续参数是匹配的组。

Thus: 从而:

alert("hello world".replace(/(ell)(o w)/, "$2$1");
// alerts "ho wellorld"

This is all about capturing groups. 这完全是关于捕获组。 ( and ) capture everything between, so you can later access substrings of your match. ()捕获之间的所有内容,以便您以后可以访问匹配的子字符串。

$1 refers to the first captured group, $2 to the second one (so first and second pair of parentheses, respectively). $1表示第一个捕获组, $2表示第二个(分别是第一对和第二对括号)。 ( $0 would refer to the whole match.) $0表示整场比赛。)

The variant with the callback function does the same, but here the variables x , y and z are filled with the captured groups (corresponding to $0 , $1 and $2 respectively). 具有回调函数的变量也是相同的,但是这里变量xyz用捕获的组填充(分别对应于$0$1$2 )。

Finally ?= opens a lookahead. 最后?=打开一个先行。 That asserts that this possition in the match is followed by <\\/script> without actually advancing the regex engine in the matched string and also without including this part in the match. 这声称在匹配中的这个可能性之后是<\\/script>而没有实际推进匹配字符串中的正则表达式引擎,也没有在匹配中包含此部分。

The $1 and $2 are referencing the captured sub-expressions (which are delimited by parentheses () ) from the regular expression in the previous argument to .replace() . $ 1和$ 2引用捕获的子表达式(由括号()分隔()从前一个参数中的正则表达式引用到.replace()

The ?= is a positive lookahead. ?=是一个积极的前瞻。 http://www.regular-expressions.info/lookaround.html http://www.regular-expressions.info/lookaround.html

The function(x, y, z) is an anonymous function expression that does a similar thing to referencing $1 and $2, but it stores them in variables instead. function(x, y, z)是一个匿名函数表达式,它与引用$ 1和$ 2类似,但它将它们存储在变量中。

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

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