The replace method allows us to replace a character, word, or sentence with a string. For example.

let str = "Hello World!";
let new_str = str.replace("Hello", "Hi");


// Result: Hi World!

To replace a value on all instances of a regular expression with a g modifier is set.

It searches for a string for a value or a regular expression and returns a new string with the value(s) replaced. It doesn't change the original string. Let's see the global case-insensitive replacement example.

let text = "Mr Blue has a blue house and a blue car";
let result = text.replace(/blue/gi, "red"); 

//Result: Mr red has a red house and a red car

results matching ""

    No results matching ""