在下面的代码中.我期待的是真的,但我却变得虚假了.我错过了什么?
var text = "Sentence $confirmationlink$ fooo";
alert(placeHolderExists(text,'confirmationlink'); // alerts false
function placeHolderExists(text,placeholdername) {
var pattern = new RegExp('\$'+placeholdername+'\$');
return pattern.test(text);
}
Run Code Online (Sandbox Code Playgroud)
ann*_*ata 12
在构建字符串时,RegExp表达式构建器中的"\"被视为转义字符,就像在实际的RegExp中一样.你需要逃脱两次,试试:
new RegExp('\\$'+placeholdername+'\\$');
Run Code Online (Sandbox Code Playgroud)