0

my string contains some variables which is given between @,eg:-(@name@). Am passing variables values and storing it in an array. I want to replace this values with the variable in actual string.

This is my code

actual_string = actual_string.replace(new RegExp('@'+dynamic[i]+"@"),value);

This will replace only one value, If i have same variable repeatedly then the above code will replace only the first occurance. Then i tried

actual_string = actual_string.replace(new RegExp('/\@'+dynamic[i]+"\@/"),value);

But this is not even replacing single variable

Anoop LL
  • 1,548
  • 2
  • 21
  • 32

1 Answers1

4

You need to use the flags parameter of RegExp. Use a g to make the search global throughout the string. Try this:

actual_string = actual_string.replace(new RegExp('@' + dynamic[i] + "@", 'g'), value);
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339