JavaScript replaceAll() method


The replaceAll() method in JavaScript is used to replace all occurrences of a specified substring or pattern in a string with a new substring. Unlike the replace() method, which replaces only the first occurrence or requires a global regular expression to replace multiple occurrences, replaceAll() is specifically designed to replace all matches.

Syntax:

string.replaceAll(searchValue, newValue)
  • searchValue: This can be either a string or a regular expression. It specifies the substring or pattern to search for in the original string. If using a regular expression, it must have the g (global) flag.
  • newValue: This is the string that will replace the matched substring(s). It cannot be a function, unlike replace().

Return Value:

  • Returns a new string with all matches of searchValue replaced by newValue. The original string remains unchanged.

Example 1: Basic Usage

let str = "Hello World! World is beautiful."; let newStr = str.replaceAll("World", "JavaScript"); console.log(newStr); // "Hello JavaScript! JavaScript is beautiful."

In this example, all occurrences of "World" are replaced with "JavaScript".

Example 2: Using Regular Expressions

You can also use regular expressions, but they must include the g flag. If you do not use the g flag, a TypeError will be thrown.

let str = "The rain in Spain stays mainly in the plain."; let newStr = str.replaceAll(/ain/g, "ane"); console.log(newStr); // "The rane in Spane stays manely in the plane."

In this case, the regular expression /ain/g replaces all instances of "ain" with "ane".

Example 3: Handling Case Sensitivity

The replaceAll() method is case-sensitive, similar to replace().

let str = "Hello World! world is beautiful."; let newStr = str.replaceAll("world", "JavaScript"); console.log(newStr); // "Hello World! JavaScript is beautiful."

Only the lowercase "world" is replaced, while the uppercase "World" remains unchanged.

Example 4: Special Characters

When using special characters in the searchValue, you may need to escape them. This is particularly important for characters that have special meanings in regular expressions.

let str = "This costs $5.00 and $10.00."; let newStr = str.replaceAll("$", "€"); console.log(newStr); // "This costs €5.00 and €10.00."

In this example, all occurrences of the dollar sign ($) are replaced with the euro sign (€).

Example 5: Performance

replaceAll() can be more efficient than using replace() with a global regular expression for replacing all occurrences, as it simplifies the syntax and avoids potential mistakes related to regex patterns.

Example 6: Chaining with Other String Methods

You can also chain replaceAll() with other string methods for more complex string manipulations.

let str = "I like cats. Cats are great!"; let newStr = str.replaceAll("cats", "dogs").toUpperCase(); console.log(newStr); // "I LIKE DOGS. DOGS ARE GREAT!"

Here, "cats" is replaced with "dogs", and the entire string is converted to uppercase.

Summary:

  • The replaceAll() method replaces all occurrences of a specified substring or pattern in a string with a new substring.
  • It can take a string or a regular expression (with the g flag) as the search value and a string as the new value.
  • The method returns a new string, leaving the original string unchanged, and is case-sensitive.
  • It simplifies the syntax for replacing multiple occurrences compared to using replace() with a global regex and can handle special characters.
  • replaceAll() does not allow the newValue to be a function; it must always be a string.