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.

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

4

1 回答 1

7

您需要为此使用RegExp构造函数:

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

这将允许您将变量传递给您的正则表达式。

于 2012-07-02T13:26:19.720 回答