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
Programmer
  • 646
  • 2
  • 11
  • 29

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
  • 138,460
  • 30
  • 271
  • 271