我无法在正则表达式中替换“joe.
”之类的内容。这是代码
var objects = new Array("joe","sam");
code = "joe.id was here so was sam.id";
for(i = 0; i < objects.length; i++) {
re = new RegExp(objects[i]+"\.", "g");
code = code.replace(re, "stan ");
}
我已尝试\.
,\\.
,\\\.
,但这些似乎都没有效果。
答案 0 :(得分:4)
您的代码与"\\."
var objects = new Array("joe","sam");
code = "joe.id was here so was sam.id";
for(i = 0; i < objects.length; i++) {
re = new RegExp(objects[i]+"\\.", "g");
code = code.replace(re, "stan ");
}
"\\."
创建符合帐单的字符串\.
。
答案 1 :(得分:1)
您可以使用[.]
代替逃避。