2012-07-02 62 views
0
var metin = "baris"; 
var icerikoku = "Hello Baris. What is surname Baris. Bye bye baris. How are you metin."; 
var n = icerikoku.replace(/metin/gi, "ziya"); 
$("#icerikoku").html(n); 

打印創建一個正則表達式:
Hello Baris. What is surname Baris. Bye bye baris. How are you ziya.使用GI標誌從變量

,但必須是:
Hello ziya. What is surname ziya. Bye bye ziya. How are you metin.

回答

6

您需要使用RegExp構造爲:

var n = icerikoku.replace(new RegExp(metin, 'gi'), "ziya"); 

這將允許您將變量傳遞到您的正則表達式。

+0

謝謝你兄弟...... – barisimnet

+1

@baris如果我的回答對你有幫助,不要忘記接受它。並且歡迎到SO;) – antyrat

+0

謝謝你男人。歡迎你自己=) – barisimnet