简体   繁体   English

\\\\ $&in“str.replace(specials,”\\\\ $&“)”是什么意思?

[英]What does \\$& in “str.replace(specials, ”\\$&“)” mean?

I am refering to code from Case insensitive string replacement in JavaScript? 在JavaScript中引用来自Case不敏感字符串替换的代码 :

RegExp.escape = function(str) 
{
  var specials = new RegExp("[.*+?|()\\[\\]{}\\\\]", "g"); // .*+?|()[]{}\
  return str.replace(specials, "\\$&");
}

What does \\\\$& mean? \\\\$&是什么意思?

I think \\\\ escapes the \\ character. 我认为\\\\逃脱了\\字符。 Then $& , I thought it should be $1 to match the 1st match? 那么$& ,我认为匹配第一场比赛应该是$1 tho $1 does not work right $1不能正常工作

$& represents the entire (sub)string matched by the regex, regardless of capture groups. $&表示正则表达式匹配的整个(子)字符串,与捕获组无关。 The replacement result you get is each match in your string being escaped by a literal backslash (represented by \\\\ ). 您获得的替换结果是字符串中的每个匹配都由文字反斜杠转义(由\\\\表示)。 Since the regex used here consists only of a character class, "each match" refers to each metacharacter listed in the character class that is matched. 由于此处使用的正则表达式仅包含字符类,因此“每个匹配”指的是匹配的字符类中列出的每个元字符。

For example, the regex string [abc] will be replaced with \\[abc\\] : 例如,正则表达式字符串[abc]将替换为\\[abc\\]

  • [ is matched as it occurs in the character class. [匹配,因为它出现在字符类中。 Represented by $& , replaced with \\[ 代表$& ,替换为\\[

  • a , b and c are not metacharacters in the character class, so they're ignored abc不是字符类中的元字符,因此它们被忽略

  • ] is matched as it occurs in the character class. ]匹配,因为它出现在字符类中。 Represented by $& , replaced with \\] $&代表,替换为\\]

$& is the entire match, which in the example is whichever special character was matched. $&是整个匹配,在示例中是匹配的任何特殊字符。 $1 doesn't work since there is no group (...) in the regex (though if you added parentheses around the square brackets, then it would also work). $1不起作用,因为正则表达式中没有组(...) (尽管如果在方括号周围添加括号,那么它也可以工作)。

Yes, the first '\\' escapes the other (usually a backslash is used in conjunction with the following character to give it special meaning, so to get a literal backslash it needs to be escaped using another backslash). 是的,第一个'\\'转义另一个(通常反斜杠与下面的字符一起使用以赋予它特殊含义,因此要获得文字反斜杠,需要使用另一个反斜杠进行转义)。

That regex is not using a capture group. 该正则表达式不使用捕获组。 Apparently he is using a common regex variable: 显然他正在使用一个常见的正则表达式变量:

$& returns the entire matched string (some systems its $0)
In this case he is just matching 1 character at a time equivalent to s/[.*+?|()\\[\\]{}\\\\]/\\\\$&/g 在这种情况下,他只匹配1个字符,相当于s/[.*+?|()\\[\\]{}\\\\]/\\\\$&/g

Obscure fact: In Perl there is a function called quotemeta("your string") that does this 模糊的事实:在Perl中有一个名为quotemeta(“你的字符串”)的函数可以做到这一点
or it can be inlined in the regex with \\Q. 或者可以使用\\ Q在正则表达式中内联。 I love Perl. 我喜欢Perl。

As a side note, he might have left out the carret ^ and possibly, but not sure, the $ 作为旁注,他可能已经遗漏了carret ^并且可能但不确定$
The fact that there is no built-in or inline method might be a bad thing. 没有内置或内联方法的事实可能是件坏事。 And escaping metachars like this must be thought out, as regex fragments can be problematic. 因为正则表达式碎片可能存在问题,所以必须考虑逃避这样的元素。

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

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