dafodil dafodil - 6 months ago 7
jQuery Question

Replacing variable in jquery

I have a string

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";


I need to put for loop where VAR_ will be replaced by NEW_VAR_ ,Where number remains same.

How could i do that in jQuery?

Answer

Try:

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);