Description

The JavaScript String method replaceAll() creates and returns a new string with all matches of the specified string/regex replaced by a replacement.

The method is case sensitive, so the case is also considered when it tries to find and replace values.

Syntax

The method repeatAll() has the below syntax, where str is a string.

str.replaceAll(pattern, replacement)

Parameters

The method replaceAll() allows the below parameters.

pattern

  • It is either a substring or a regular expression that determines what to replace.

replacement

  • The pattern is replaced with this replacement.
  • It can be a string or a function that returns a replacement string.

Result

The method replaceAll() returns a new string with all matches of the specified string/regex replaced by a replacement.

A RegExp without a global switch g throws a TypeError.

Example 1: Replace All Occurrences

By default, the method replaces all the occurrences if the string exactly matches the pattern including its case.

If it is a simple string replacement, we can directly use a string or a regular expression.

var str = "Hello hello HELLO Hello hello HELLO";
var searchString = "hello";
var searchPatternGlobal = /hello/g;

// Using a string
document.write(str.replaceAll(searchString, "world"));    // Prints: Hello world HELLO Hello world HELLO

// Using a simple pattern with global search
document.write(str.replaceAll(searchPatternGlobal, "world"));    // Prints: Hello world HELLO Hello world HELLO

Output:

Hello world HELLO Hello world HELLO
Hello world HELLO Hello world HELLO

Example 2: Method Throws TypeError

If we use a RegExp to search for a pattern, then we always need to use a global switch g. Otherwise, the method returns a TypeError.

var str = "Hello hello HELLO Hello hello HELLO";
var pattern = /hello/;
var patternGlobal = /hello/g;

// Using a pattern for global search
document.write(str.replaceAll(patternGlobal, "world"));   // Prints: Hello world HELLO Hello world HELLO

// Using a simple pattern
document.write(str.replaceAll(pattern, "world"));         // Throws: TypeError

Output:

Hello world HELLO Hello world HELLO
TypeError: String.prototype.replaceAll called with a non-global RegExp argument

Example 3: Replace with Ignoring Case

By default, the method is case-sensitive, so the replacement happens only if the case is matched.

If we want to perform a case-insensitive replacement, then we need to use a regex with i a switch for a case-insensitive search.

var str = "Hello hello HELLO Hello hello HELLO";
var pattern = /hello/g;
var patternCaseInsensitive = /hello/gi;

// Using a simple pattern
document.write(str.replaceAll(pattern, "world"));    // Prints: Hello world HELLO Hello world HELLO

// Using a pattern for case insensitive search
document.write(str.replaceAll(patternCaseInsensitive, "world"));    // Prints: world world world world world world

Output:

Hello world HELLO Hello world HELLO
world world world world world world

Example 4: Passing Function as a Replacement

The method allows a function (instead of a string) as a replacement, where the function return value is used for replacement.

var str = "Random value is: 3.1415";
var pattern = /\d/g;

// Function to generate a random digit between 0 and 9
function generateRandomDigit() {
  return Math.floor(Math.random() * 10);
}

document.write(str.replaceAll(pattern, generateRandomDigit));
document.write(str.replaceAll(pattern, generateRandomDigit));
document.write(str.replaceAll(pattern, generateRandomDigit));

Output:

Random value is: 4.8824
Random value is: 3.6029
Random value is: 9.4004

Overall

The JavaScript String method replaceAll() creates and returns a new string with all matches of the specified string/regex replaced by a replacement.

Related Links