Open In App

JavaScript String replaceAll() Method

Last Updated : 16 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

The replaceAll() method in JavaScript is used to replace all occurrences of a specified substring or pattern with a new substring. The replaceAll() method does not change the original string.

JavaScript’s replaceAll() method used for replacing all instances of a specified substring or pattern within a string with a new substring. This method comprehensively updates the string by replacing every occurrence of the target substring or pattern with the provided replacement string.

Note: The original string remains unchanged, preserving its integrity throughout the process.

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 be replaced 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.

Return Value:

Returns a String where the search value has been replaced.

Example 1: String Replace All Occurrences

The function gfg() takes a string “Geeks or Geeks”, replaces all occurrences of “or” with “for”, and logs the modified string “Geeks for Geeks” to the console.

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

Output
Geeks for Geeks

Example 2: Replace All Occurrences of “coffee” with “tea”

The function GFG() defines a regular expression /coffee/ig to match all occurrences of “coffee” case-insensitively in the string “Lets, have coffee today!”. It then replaces all occurrences with “tea” and logs the modified string “Lets, have tea today!” to the console.

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 Browsers:

JavaScript String replaceAll() Method – FAQs

What does replaceAll() do?

The replaceAll() method in JavaScript replaces all occurrences of a specified substring with another string. Unlike replace(), which replaces only the first occurrence, replaceAll() replaces all occurrences globally within the string.

How does replaceAll() differ from replace()?

replaceAll() differs from replace() by replacing all instances of a substring, not just the first one. This global replacement behavior makes it convenient for bulk string transformations without needing to use regular expressions.

Can replaceAll() handle regular expressions?

Yes, replaceAll() accepts regular expressions as its search pattern. This allows for advanced string replacement operations based on patterns rather than fixed substrings, enhancing its flexibility in text processing tasks.

Does replaceAll() modify the original string?

No, replaceAll() does not modify the original string; instead, it returns a new string with all replacements applied. This ensures that the original string remains unchanged, adhering to the immutability principle of strings in JavaScript.

Is replaceAll() case-sensitive?

Yes, replaceAll() is case-sensitive by default. It replaces occurrences based on exact matches of the substring or regular expression pattern provided. To perform case-insensitive replacements, regular expressions with flags like /i can be used.

Does replaceAll() support callback functions?

No, unlike some other string manipulation methods in JavaScript (replace() with a function), replaceAll() does not directly support callback functions for replacements. It is designed primarily for simple string-to-string replacements.



Next Article

Similar Reads

three90RightbarBannerImg