找 。正则表达式问题中的字符

时间:2012-01-26 21:48:24

标签: javascript regex

我无法在正则表达式中替换“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 ");  
}

我已尝试\.\\.\\\.,但这些似乎都没有效果。

2 个答案:

答案 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)

您可以使用[.]代替逃避。