jquery - Replacing all match in a String -
this question has answer here:
my string contains variables given between @,eg:-(@name@)
. passing variables values , storing in array. want replace values variable in actual string.
this code
actual_string = actual_string.replace(new regexp('@'+dynamic[i]+"@"),value);
this replace 1 value, if have same variable repeatedly above code replace first occurance. tried
actual_string = actual_string.replace(new regexp('/\@'+dynamic[i]+"\@/"),value);
but not replacing single variable
you need use flags
parameter of regexp
. use g
make search global throughout string. try this:
actual_string = actual_string.replace(new regexp('@' + dynamic[i] + "@", 'g'), value);
Comments
Post a Comment