String.prototype.replaceAll(pattern, replacement) returns a new string where every match is swapped. A plain string pattern replaces all occurrences (unlike replace()). A RegExp must include the g flag.
Pass the required arguments shown below. Check return values and browser support before you rely on the result.
Syntax
Call string.replaceAll. Start with this line.
JavaScript
str.replaceAll(pattern, replacement)
Call
What it does
str.replaceAll(pattern, replacement)
Primary signature.
string.replaceAll(...)
Call with the arguments your case needs.
Built into JavaScript. Call replaceAll(...) on a string (or String instance) — no import required.
Library declarations
JavaScript
str.replaceAll(pattern, replacement)
Parameters
Parameter
Description
pattern
Required. Substring, or RegExp with g
replacement
Required. New text, or a function returning text
Return Value
Return Value
Description
*
The value returned by string.replaceAll().
JavaScript Examples
Example 1 — Replace Every Literal Match
A string pattern swaps every occurrence — no regex required.
Pass a plain string."world" is treated as literal text.
Swap every hit. Both "world" occurrences become "JS".
Works for characters too. Every "b" in "aabbcc" becomes ".".
Original unchanged. Always use the returned string.
Example 2 — replaceAll() vs replace()
Same inputs, different “how many matches” rules.
JavaScript
const str = "a a a";
console.log(str.replaceAll("a", "b")); // "b b b"
console.log(str.replace("a", "b")); // "b a a"
console.log(str.replaceAll(/a/g, "b")); // "b b b"
console.log(str.replace(/a/g, "b")); // "b b b"
Require global. Without g, replaceAll throws TypeError.
Add the flag./b/g replaces every b.
Combine with i./apples/gi matches any capitalization.
Or skip regex. For plain text, a string pattern is simpler and safer.
Compatibility
Browser & Runtime Support
String.prototype.replaceAll() is part of ES2021 and is Baseline Widely available across modern browsers.
✓ Baseline · ES2021
String.prototype.replaceAll()
Supported in Chrome 85+, Firefox 77+, Safari 13.1+, Edge 85+, and Node.js 15+. Not available in Internet Explorer.
95%Modern browser support
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
String.replaceAll()Excellent
Bottom line: Safe for modern web apps and current Node LTS releases. Feature-detect with if ("replaceAll" in String.prototype) and fall back to str.replace(/pat/g, rep) for older targets.
Frequently Asked Questions
replaceAll(pattern, replacement) returns a new string where every match of pattern is swapped for replacement. Unlike replace() with a string pattern, it replaces all occurrences — not just the first.
No. Strings are immutable. replaceAll() returns a new string; the original stays the same.
replaceAll() throws TypeError. A regex pattern must include the global (g) flag.
Use replaceAll when you want every match — especially for plain string patterns. Use replace when you only want the first match.
Yes for literal text. A string pattern avoids treating characters like . * + as regex metacharacters.
An empty string pattern inserts the replacement between every UTF-16 code unit. For example "xxx".replaceAll("", "_") returns "_x_x_x_".