当使用美元符号($)作为replace时“string.replace`奇怪的行为

我在我的JavaScript代码中发现了一个错误,我将其隔离为一个stringreplace,这是我没有想到的方式。 这是一个代码示例:

var text = "as"; text = text.replace(text,"$\'"); console.log(text); 

这将打印一个空string到控制台。 我期待它打印$'到控制台。 任何人都可以解释吗?

为了在结果string中使用$$ ,使用$$作为$在JavaScript正则expression式和stringreplace方法中具有特殊含义: https : //developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_string_as_a_parameter

如果我不知道我使用的replacestring是什么

 replaceWith = "might have 2 $ signs $$ $$$ $$$$" "abc".replace("b", replaceWith) // unexpected result "abc".replace("b", function(){return replaceWith}) // no surprises