The Wayback Machine - https://web.archive.org/web/20240118181515/https://www.geeksforgeeks.org/javascript-string-replaceall-method/
Open In App
Related Articles

JavaScript String replaceAll() Method

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The Javascript replaceAll() method returns a new string after replacing all the matches of a string with a specified string or a regular expression. The original string is left unchanged after this operation.

Syntax:

const newString = originalString.replaceAll(regexp | substr , newSubstr | function)

Parameters: This method accepts certain parameters defined below: 

  • regexp: It is the regular expression whose matches are replaced with the newSubstr or the value returned by the specified function.
  • substr: It defines the substrings which are to replace with newSubstr or the value returned by the specified function.
  • newSubstr: It is the substring that replaces all the matches of the string specified by the substr or the regular expression.
  • function: It is the function that is invoked to replace the matches with the regexp or substr.

Example: Below is an example of the String replaceAll() Method.

Javascript




function gfg() {
    let string = "Geeks or Geeks";
    newString = string.replaceAll("or", "for");
    console.log(newString);
}
gfg();


Output:

Geeks for Geeks

More examples of the replaceAll() method are given below:

Example 1: In this example, we will replace all occurrences of the Hello word with Hi using the replaceAll() method in Javascript.

Javascript




function GFG() {
    let string = "Hello, what are you doing?";
    newString = string.replaceAll("Hello", "Hi");
    console.log(newString);
}
GFG();


 Output :

Hi, what are you doing?

Example 2: In this example, we will replace all occurrences of the coffee world with tea using the replaceAll() method in Javascript.

Javascript




function GFG() {
    const regexp = /coffee/ig;
    let string = "Lets, have coffee today!";
    newString = string.replaceAll(regexp, "tea");
    console.log(newString);
}
GFG();


 Output:

Lets, have tea today!

We have a complete list of Javascript string methods, to check those please go through the Javascript String Complete Reference article.

Supported Browser:

  • Chrome 85 and above
  • Edge 85 and above
  • Firefox 77 and above
  • Opera 71 and above
  • Safari 13.1 and above

Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 19 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials