JavaScriptStringsBeginner

String.replaceAll()

Replaces all occurrences of a string/regex and returns a new string.

Review the syntaxStudy the examplesOpen the coding app
string.replaceAll(searchValue, replaceValue)

This static page keeps the syntax and examples indexed for search, while the coding app handles interactive exploration and saved references.

What it does

Overview

Replaces all occurrences of a string/regex and returns a new string.

The replaceAll() method returns a new string where all occurrences of a specified pattern are replaced by a replacement string or the result of a replacement function. Unlike the standard replace() method, which only replaces the first instance when given a string as the search pattern, replaceAll() is exhaustive and targets every match found. A critical technical constraint is that if the search pattern is a Regular Expression, it must include the global ('g') flag; omitting this flag will result in a TypeError. This method provides better performance and clearer intent compared to legacy workarounds like 'split().join()' or manually escaping strings for regex. Because it was introduced in ES2021, Internet Explorer is not supported. The original string remains immutable, and the method follows standard Unicode mapping for character matching.

Quick reference

Syntax

string.replaceAll(searchValue, replaceValue)

Inputs

Parameters

searchValuestring | RegExp · What to replace.
replaceValuestring | Function · Replacement string or function.

See it in practice

Examples

1

Replacing all instances of a substring

const quote = 'Blue is my favorite color. Blue is everywhere.';
const newQuote = quote.replaceAll('Blue', 'Green');
console.log(newQuote);
Output:
"Green is my favorite color. Green is everywhere."

Replaces every literal instance of 'Blue' with 'Green' without using a Regular Expression.

2

Using a global Regular Expression

const data = '100px, 200px, 300px';
const numericOnly = data.replaceAll(/[a-z]/g, '');
console.log(numericOnly);
Output:
"100, 200, 300"

Uses a global regex to remove all lowercase letters. The 'g' flag is required when using replaceAll with regex.

3

Dynamic replacement via callback function

const prices = 'Prices: 5, 10, 15';
const doubled = prices.replaceAll(/\d+/g, (match) => {
  return parseInt(match) * 2;
});
console.log(doubled);
Output:
"Prices: 10, 20, 30"

Applies a transformation logic to every match found in the string using a callback function.

Debug faster

Common Errors

1

TypeError

Cause: Calling replaceAll with a non-global Regular Expression (missing the 'g' flag).

Fix: Ensure the Regular Expression includes the 'g' flag, even if you only expect one match, or use a string literal.

'test'.replaceAll(/t/, 'x'); // Throws TypeError
2

LogicError

Cause: Assuming the method modifies the original string in-place.

Fix: Assign the returned value to a new variable or overwrite the existing one, as strings are immutable.

let text = 'cat'; text.replaceAll('c', 'b'); console.log(text); // Still 'cat'

Runtime support

Compatibility

Node.js, BrowserAll modern browsers (recent), IE not supportedES2021

Source: MDN Web Docs

Common questions

Frequently Asked Questions

Replaces all occurrences of a string/regex and returns a new string.

searchValue: What to replace. replaceValue: Replacement string or function.

TypeError: Ensure the Regular Expression includes the 'g' flag, even if you only expect one match, or use a string literal. LogicError: Assign the returned value to a new variable or overwrite the existing one, as strings are immutable.