下面的脚本返回空值。如果我在str和patt1中删除文本spot_Northeast(300)_comment的圆括号,它会正确返回

<script type="text/javascript">
var str="0000CentralGasEform2,0000CentralGasEformNew,spot_Northeast(300)_comment";
var patt1=/\bspot_Northeast(300)_comment/g;
document.write(str.match(patt1));
</script>

在我的应用程序中,str中的值取自cookie,而patt1中的值基于打开的窗口是动态的
regExp = new RegExp('\\b'+FormOpen+'\\b', 'g');

在这里,FormOpen是动态的。然后我将匹配项替换为Cookie
var temp=CookieList.replace(regExp,"");

有没有办法告诉RegEX()是普通文本,而不是硬编码?

最佳答案

逃脱他们
var patt1=/\bspot_Northeast\(300\)_comment/g;
()是上下文(。*)中的保留字符,例如,您需要通过称为转义的过程告诉正则表达式引擎将其视为普通文本

10-04 18:47