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.

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

Popular posts from this blog

Install Node.js without admin rights

Create a lean React Solution using Typescript

Session Storage Methods with Expiry