1

Possible Duplicate:
replace all occurrences in a string

My code:

alert('111'.replace('1','2'));

It is giving result as 211. But I need the result as 222.

Community
  • 1
  • 1
Earth
  • 3,477
  • 6
  • 37
  • 78

1 Answers1

12

In order to replace all occurrences you can use regular expression with g (i.e. global) flag:

"111".replace(/1/g, "2");
VisioN
  • 143,310
  • 32
  • 282
  • 281