Horje
Replace all Occurrences of a Substring in a String in JavaScript

In JavaScript, a string is a sequence of characters represented with quotation marks. We are given a string and we need to replace all occurrences of a substring from the given string.

Using the replace() method with regular expression

  • First, declare a string variable str with the value “GeeksforGEEKS”.
  • Now, declare a substring variable with the value “GEEKS” to search for and replace.
  • Use the replace method with a regular expression to globally replace all instances of the substring with the replacement value “geeks”.
  • It logs the result which will be “Geeks For geeks “.

Example: The below code will explain the use of replace() method with regular expression.

Javascript

let str = "GeeksforGEEKS";
let substring = "GEEKS";
let replacement = "Geeks";
console.log("Before replacement: ", str)
let finalResult =
    str.replace(new RegExp(substring, "g"),
        replacement);
 
console.log("After Replacement: ", finalResult);

Output

Before replacement:  GeeksforGEEKS
After Replacement:  GeeksforGeeks

Using indexOf() and slice() methods

  • The indexOf() method finds the index of the first occurrence of the substring.
  • If the index is greater than or equal to 0 (found a match), it slices the string before and after the match and inserts the replacement.
  • It recursively calls itself to keep replacing all other matches.
  • It returns the final string after replacing all matches.

Example: The below code will explain the use of the indexOf() and slice() methods to replace all occurences of a substring.

Javascript

function replaceAll(str, substring, replacement) {
    let index = str.indexOf(substring);
    if (index >= 0) {
        str =
            str.slice(0, index) + replacement +
            str.slice(index + substring.length);
        str = replaceAll(str, substring, replacement);
    }
    return str;
}
const str = "GeeksforGEEKS";
const subStr = "GEEKS";
const replacement = "Geeks";
console.log("Before replacement: ", str)
let finalResult =
    replaceAll(str, subStr, replacement);
console.log("After Replacement: ", finalResult);

Output

Before replacement:  GeeksforGEEKS
After Replacement:  GeeksforGeeks

Using replaceAll() method

  • Declare a constant str variable with the initial string “GeeksforGEEKS”.
  • Now, use the String.replaceAll() method to replace all occurrences of “GEEKS” in the str with “Geeks”, assigning the result to finalResult and logging it to the console.

Example: The below code is practical implementation of the above-discussed approach.

Javascript

const str = "GeeksforGEEKS";
const finalResult =
    str.replaceAll("GEEKS", "Geeks");
console.log("Before replacement: ", str)
console.log("After Replacement: ", finalResult);

Output

Before replacement:  GeeksforGEEKS
After Replacement:  GeeksforGeeks



Reffered: https://www.geeksforgeeks.org


JavaScript

Related
How to Declare Variables in TypeScript ? How to Declare Variables in TypeScript ?
Explain the Concept of Interfaces in TypeScript ? Explain the Concept of Interfaces in TypeScript ?
What is the use of Union Type in TypeScript ? What is the use of Union Type in TypeScript ?
How to implement Type narrowing in TypeScript? How to implement Type narrowing in TypeScript?
JavaScript Program to Search an Element in an Array JavaScript Program to Search an Element in an Array

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
11