在jquery中替换变量

时间:2016-06-10 04:17:09

标签: jquery

我有一个字符串

var DisplayString = "I am having VAR_1 value in box, i have to change to VAR_2, then to VAR_3, then to VAR_4";

我需要进行循环,其中VAR_将被NEW_VAR_替换,其中数字保持不变。

我怎么能在jQuery中做到这一点?

1 个答案:

答案 0 :(得分:0)

尝试:

var DisplayString = "I am having VAR_1 value in box, i have to change to VAR_2, then to VAR_3, then to VAR_4";
DisplayString = DisplayString.replace(/\VAR_/g, 'NEW_VAR_'); // this will replace all occurances of VAR_ to NEW_VAR_
alert(DisplayString);