What does \\ $ & in "str.replace (specials," \\ $ & ")" mean?

I mean code from case-insensitive string replacement in JavaScript? :

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

What does it mean \\$&?

I think that \\goes beyond the symbol \. Then $&, I thought that should $1correspond to the 1st coincidence? tho $1not working correctly

+3
source share
3 answers

$& () , , . , , - , ( \\). , " " , .

, [abc] \[abc\]:

  • [ , . $&, \[

  • a, b c ,

  • ] , . $&, \]

+4

$& - , . $1 , (...) (, , ).

, "\" ( , , ).

0

. -, :

$& returns the entire matched string (some systems its $0)
1 s/[.*+?|()\[\]{}\\]/\\$&/g

An unclear fact: in Perl, there is a quotemeta function ("your string") that does this or it can be embedded in a regular expression with \ Q. I love Perl.

As a side note, he may have abandoned carret ^ and perhaps, but not sure, $
The fact that there is no built-in or built-in method can be bad. And to avoid metahars like this one needs to be thought out, because fragments of regular expressions can be problematic.

0
source

Source: https://habr.com/ru/post/1785365/


All Articles