CKTIS / JavaScript
String.prototype.replaceAll — replacing all matches
replaceAll replaces every non-overlapping match of a string or global regular expression without changing the original string. The replacement can be text or a function.
Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support
Syntax
text.replaceAll(searchValue, replaceValue)For a string pattern, String.prototype.replaceAll returns a string with every non-overlapping match replaced from left to right. It leaves the original unchanged and does not search inserted text again. With no matches, the result has unchanged contents. Unlike replace with a string pattern, it does not stop after the first match.
A string searchValue is literal: "." means a period, not any character. A regular expression must have the g flag; omitting it causes a TypeError. An empty string pattern matches every UTF-16 code-unit boundary, including the start and end: "ab".replaceAll("", "-") produces "-a-b-". An object with a [Symbol.replace] method can take over the operation and determine its result.
replaceValue can be text or a function. In replacement text, $$ means a dollar sign and $& means the match; regular expressions also support capture-group references. To insert dynamic text containing these sequences literally, use () => replacement. For a string pattern, the function receives (match, offset, string); for a regular expression, it additionally receives capture groups before the offset and a named-groups object at the end if named groups exist.
The method belongs to ECMAScript 2021, not Web APIs. The specification is not a browser compatibility table; Node 24 being present in a repository does not establish browser support. Check the target environment, for example with typeof String.prototype.replaceAll === "function".
Examples
const text = "a.b.a";
console.log(text.replaceAll(".", "-"));
console.log(text.replaceAll(/a/g, "A"));
console.log(text.replaceAll("a", () => "$&"));
console.log(text);Result
a-b-a A.b.A $&.b.$& a.b.a
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.