Encodera

Thanks for reading!

Donate to the site — we work hard for you.

JavaScript reference

String.prototype.replaceAll

Replaces all occurrences of a substring with another string.

Available since: ES2021

Syntax

str.replaceAll(pattern, replacement)

Parameters

ParameterTypeDescription
patternstring | RegExpWhat to look for: a string or a regular expression with the `g` flag.
replacementstring | FunctionThe replacement.

Return value

stringA new string with the replacements applied.

Examples

Replacing every occurrence:

console.log('a-b-c'.replaceAll('-', '+'));

Output:

a+b+c

replace() with a string replaces only the first occurrence:

console.log('a-b-c'.replace('-', '+'));

Output:

a+b-c

Notes

The search is case-sensitive. For a case-insensitive replacement, use a regular expression with the gi flags.

See also

Related recipes

In the tutorial

  • String methods — Useful string methods in JavaScript: finding a position, reading characters, padStart, repeat, split and join, method chains.

Thanks for reading!

Donate to the site — we work hard for you.