2011-11-16 133 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")