Replace all occurence of String in JavaScript
Hi All,
I came across an issue in JavaScript where i was not able to replace all the occurrences of a string in another string.
A simple string.replace would only replace the first occurence.
We can make a small tweak in the existing code and make it work.Below is an example.
I came across an issue in JavaScript where i was not able to replace all the occurrences of a string in another string.
A simple string.replace would only replace the first occurence.
str.replace("hello", 'hi');
We can make a small tweak in the existing code and make it work.Below is an example.
str.replace(new RegExp('hello', 'g'), 'hi');
Comments
Post a Comment