0

How do I replace all occurrences of a string in JavaScript? I tried:

string.replaceAll('aa', 'b')
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135

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
  • 33,923
  • 10
  • 53
  • 80