-1

I have a problem with REPLACE method for strings . I have a string witch contains 1 or 2 or ... same string and I want to replace all of them to one thing but replace method doesn't work , follow this :

var a = "hihi";
b = a.replace("hi","hello");
alert(b);

It should alert hellohello but it alerts hellohi .

Please help . Thanks .

  • Pease do not minus this question, while question asker could have written it better it's a legitimate question, since MDN documentation doesn't match behavior in Chrome (and maybe other browsers) – David Sergey Nov 14 '13 at 13:36

1 Answers1

2

Use a regular expression with the global g set to replace all occurrences in the string.

var string = 'hihi';
var result = string.replace(/hi/g, 'hello');
alert(result); // 'hellohello'
Mathias Bynens
  • 144,855
  • 52
  • 216
  • 248