Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

JavaScript · Function

replaceAll

String.prototype.replaceAll() returns a new string in which every match of a string or replacement pattern is replaced. It leaves the original string unchanged.

How it works

Call replaceAll with a string or replacement pattern and a replacement string or callback. It produces a new string containing the replacements; a regular-expression pattern must be global, otherwise the call throws a TypeError.

Syntax

replaceAll(pattern, replacement)

Parameters

NameTypeDescription
patternstring | RegExp | objectA string, regular expression, or object with a Symbol.replace method to find.
replacementstring | functionA replacement string or function used for each match.

Returns

A new string with all matches of the pattern replaced by the replacement.

Key points

  • It replaces every match and returns the resulting string.
  • Patterns support strings and regular expressions; replacements support strings and callbacks.
  • A string pattern is globally replaced without needing a regular-expression flag.
  • Objects implementing Symbol.replace can customize the operation.
  • Non-Symbol.replace pattern values are converted to strings.

Common mistakes

  • A RegExp pattern must include the global g flag.
  • The receiver is immutable; retain the returned string if you need the changed value.

Example: Replace every string occurrence

const input = "red, red, blue";
const output = input.replaceAll("red", "green");

console.log(output); // "green, green, blue"

Every occurrence of the string pattern is replaced, while the original string remains unchanged.

When to use it

Replace a literal token everywhere

Sanitize or normalize repeated text tokens in a string.

Perform global regular-expression replacement

Apply pattern-based transformations across all matching substrings.

Generate replacements with a callback

Compute match-specific output using capture information or surrounding text.

Interview questions

QHow does replaceAll differ from replace?

replaceAll returns a new string with every match of the pattern replaced. Unlike replace with a string pattern, it does not stop after the first occurrence.

Follow-up: What happens with a non-global RegExp? It throws a TypeError because replaceAll requires the g flag.

QWhat types of arguments does replaceAll accept?

The pattern may be a string or an object providing Symbol.replace, commonly a RegExp. The replacement may be a string or a function invoked for each match.

Follow-up: Why can a custom object participate? replaceAll can call that object's Symbol.replace method with the target string and replacement.

QWhat does replaceAll return, and does it mutate the source?

The method leaves the original string unchanged and returns the resulting replacement string. A string pattern is converted as needed, while a regular expression must be global.

Follow-up: When is replace preferable? Use replace when only one replacement is intended or when a non-global regular expression is appropriate.

  • String.prototype.replace()
  • RegExp.prototype[Symbol.replace]()
  • Symbol.replace

Source: String.prototype.replaceAll() - JavaScript - MDN Web Docs. Checked 2026-10-15.