为什么此代码无法正常工作?
var temp = "@TEMP (A1)"
var text = "1st Oct @TEMP (A1)"
text = text.replace(new RegExp(temp, "gi"), "");
console.log(text); //I get same text even though I used replace instead of 1st Oct??
谁能解释这是怎么回事?
最佳答案
您需要引用temp
的特殊字符,该字符直接用作您的正则表达式。 (
和)
字符将字符分组到模式,实际上不匹配'('和')'。