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.

最佳答案

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

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


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

关于javascript - 使用变量的gi标志创建正则表达式,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11294630/

10-11 11:46