var my_string = "some text goes here!!!";
Why is it that my_string.replace('!', '*', my_string); only gives
some text goes here!!*
instead of some text goes here*** ?
Any idea?
asked Mar 29, 2013 at 15:25
lomse
4,1668 gold badges51 silver badges68 bronze badges
-
2Add global fla g.dfsq– dfsq2013年03月29日 15:26:41 +00:00Commented Mar 29, 2013 at 15:26
3 Answers 3
By default replace() only replaces the first occurrence. To replace all occurrences, pass in the global flag, as in:
var my_string = str.replace(/!/g,"*");
answered Mar 29, 2013 at 15:28
qbert65536
4594 silver badges9 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
you can perform a global replacement by using g..
The g modifier is used to perform a global match (find all matches rather than stopping after the first match).
var replaced_string= my_string.replace(/!/g, '*');
answered Mar 29, 2013 at 15:28
sasi
4,3784 gold badges30 silver badges48 bronze badges
Comments
You need to use the global flag g. This should suit your needs:
.replace(/!/g, '*');
answered Mar 29, 2013 at 15:30
sp00m
49k31 gold badges152 silver badges263 bronze badges
Comments
lang-js