Replace All Occurrences of a String in JavaScript

Replace All Occurrences of a String

In this example, you will learn to write a JavaScript program that will replace all occurrences of a string.

To understand this example, you should have the knowledge of the following JavaScript programming topics:

JavaScript String

JavaScript Regex


Example 1: Replace All Occurrence of String Using RegEx

In the above program, a regex expression is used as the first parameter inside the replace() method.

/g refers to global (that replacement is done across the whole string) and /i refers to case-insensitive.

The replace() method takes the string that you want to replace as the first parameter and the string you want to replace with as the second parameter.


Example 2: Replace All Occurrence of String Using built-in Method


In the above program, the built-in split() and join() method is used to replace all the occurrences of the string.

The string is split into individual array elements using the split() method.

Here, string.split('red') gives ["Mr ", " has a ", " house and a ", " car"] by splitting the string.

The array elements are joined into a single string using the join() method.

Here, reverseArray.join('blue') gives Mr blue has a blue house and a blue car by joining the array elements.

Post a Comment

Previous Post Next Post