2011-11-16 138 views
0

此代码中的模式不会替换括号。我也试过“/(|)/ g”。Javascript替换不会替换

var re = "/[^a-z]/g", 
    txt = navsel.options[i].text.split(" ")[0], // here I get the text from a select and I split it. 
    // What I expect is strings like "(en)" , "(el)" etc 
    txt = txt.replace(re," ") 

在此先感谢

+4

什么是你输入的文字和你有什么期望的输出是什么? –

+0

编辑,对不起,我没有指定 – chchrist

回答

5

你的正则表达式是一个字符串,这将尝试更换该字符串完全相同。正则表达式对象周围没有引号,只是分隔符。试着这样说:

var re = /[^a-z]/g, 
    txt = navsel.options[i].text.split(" ")[0], // here I get the text from a select and I split it. 
    txt = txt.replace(re," "); 
+0

OMG,非常感谢你! – chchrist

+0

不客气:-) –

3

或者如果你喜欢的字符串(和更明确的类型):

var re = new RegExp("[^a-z]", "g")