JavaScript (ES2026) reference · String Methods

JavaScript String replaceAll() Method

Replace every occurrence of a string (or regex with the g flag) with new text, without writing a regex.

JavaScript (ES2026) Since ES2021

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

string.replaceAll(pattern, replacement)

Parameters

ParameterDescription
pattern A string, or a regular expression that has the g flag.
replacement A string or a function.

Return value

A new string.

Examples

Replace every occurrenceTry it yourself
console.log("a-b-c".replaceAll("-", "/"));

Output

a/b/c
Regex must be globalTry it yourself
try {
  "aaa".replaceAll(/a/, "b");
} catch (e) {
  console.log(e.constructor.name, "- regex needs the g flag");
}
console.log("aaa".replaceAll(/a/g, "b"));

Output

TypeError - regex needs the g flag
bbb

Version history

VersionChange
JavaScript ES2021Added

In other languages

Was this page helpful?