Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
var my_string = "some text goes here!!!";
Why is it that my_string.replace('!', '*', my_string); only gives
my_string.replace('!', '*', my_string);
some text goes here!!*
instead of some text goes here*** ?
some text goes here***
Any idea?
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,"*");
Add a comment
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, '*');
You need to use the global flag g. This should suit your needs:
g
.replace(/!/g, '*');
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.