Is it necessary to escape the replacement string in a String.replace() call with a regular expression?

前端 未结 2 1374
梦毁少年i
梦毁少年i 2021-01-21 09:45

In wonder if it is necessary to escape certain chars in the replacement string of a replace operation in Javascript. What I have is this:

let t = \"##links##\";
         


        
2条回答
  •  孤独总比滥情好
    2021-01-21 10:01

    Dollar sign ($) is special in replace. If you want a single, literal dollar sign, use $$. Otherwise, the replacement string can include the following special replacement patterns:

    • $$ Inserts a $.
    • $& Inserts the matched substring.
    • $` Inserts the portion of the string that precedes the matched substring.
    • $' Inserts the portion of the string that follows the matched substring.
    • $n Where n is a positive integer less than 100, inserts the nth parenthesized submatch string, provided the first argument was a RegExp object.

提交回复
热议问题