This article talks about how to replace all occurrences of a string in javascript using different methods and various examples.

Table of Contents:-

Replace all occurrences of string case-insensitive in javascript using replace()

The replace() method in javascript looks for a pattern and replaces some or all of its occurrences with a replacement(string). The pattern can be a string or regExp.

Syntax:-

replace(regexp, newSubString)
replace(subString, newSubString)

Example:-

Replace all occurrences of “This” with “Javascript”

let dummyString = "This is a language, This is the most popular language. This is easy to learn. This is text-based."
dummyString = dummyString.replace(new RegExp("this", "gi"), "Javascript")
console.log(dummyString)

OR

let dummyString = "This is a language, This is the most popular language. This is easy to learn. This is text-based."
dummyString = dummyString.replace(new RegExp("This", "gi"), "Javascript")
console.log(dummyString)

Output:-

Javascript is a language, Javascript is the most popular language. Javascript is easy to learn. Javascript is text-based.

Here in the replace() function, an object of RegExp is passed with “this” -> first argument, , “gi” -> second argument to identify the pattern “this”. “gi” specifies that the replacement should be case insensitive.

Replace all occurrences of string case insensitive in javascript using replaceAll()

The replaceAll() method in javascript returns a new string with all matches of a pattern replaced by a replacement(string).

Syntax:-

replaceAll(regexp, newSubString)
replaceAll(subString, newSubString)

Example:-

Replace all occurrences of “This” with “Javascript”

let dummyString = "This is a language, This is the most popular language. This is easy to learn. This is text-based."
dummyString = dummyString.replaceAll(/this/gi, "Javascript")
console.log(dummyString)

Output:-

Javascript is a language, Javascript is the most popular language. Javascript is easy to learn. Javascript is text-based.

Replace all occurrences of string in javascript using split and join

This section will replace all occurrences of a string in javascript by splitting and joining an array. The solution is to split the original string by the search string and then replace them with a new substring. Then, finally, joining back into one string.

Syntax:-

newString = originalString.split(subString).join(newSubString)

Example:-

Replace all occurrences of “This” with “Javascript”

let dummyString = "This is a language, This is the most popular language. This is easy to learn. This is text-based."
dummyString = dummyString.split("This").join("Javascript");
console.log(dummyString)

Output:-

Javascript is a language, Javascript is the most popular language. Javascript is easy to learn. Javascript is text-based.

Replace all occurrences of string in javascript using substring and indexOf

In this section, we will replace all occurrences of a string in javascript by:

  • Finding the indexOf of a pattern.
  • Identifying the substring to be replaced based on the index and then replacing it with the new substring.
  • Finally, concatenating the substrings to one string.

Example:-

Replace all occurrences of “This” with “Javascript”

let dummyString = "This is a language, This is the most popular language. This is easy to learn. This is text-based."
let i=-1;
let findToken = 'This'
let newSubString ='Javascript'
     while (( i=dummyString.indexOf(findToken,i>=0? i=newSubString.length : 0 )) !== -1
         )
         { 
        dummyString= dummyString.substring(0,i) + newSubString + dummyString.substring(i+findToken.length)
         } 
console.log(dummyString)

Output:-

Javascript is a language, Javascript is the most popular language. Javascript is easy to learn. Javascript is text-based.

Read More:

We hope this article helped you to replace all occurrences of a string in javascript. Good Luck !!!