0

How do I replace all occurrences of a string in javascript? I've tried the following code:

string.replaceAll('aa', 'b')

but it doesn't seem to have worked

1 Answers1

0

If you use a regex with the g flag you can replace all instances in one replace call.

var str = ...;
var regex = /aa/g;

str.replace(regex, 'b');
ControlAltDel
  • 32,042
  • 9
  • 48
  • 75