String.prototype.replaceAll() returns a new string with every match of a pattern replaced (same API as MDN String.prototype.replaceAll()). Learn how it differs from replace(), why regexes must use the g flag, empty-string behavior, five examples, and try-it labs.
01
Kind
Instance method
02
Returns
New string
03
String pattern
All matches
04
Regex
Must have g
05
Mutates?
No
06
Baseline
Widely available
Fundamentals
Introduction
Want every copy of a word swapped — not just the first? replaceAll() is the clear tool for that. Pass a plain string and every occurrence is replaced. Pass a regex and it must include the g flag, or JavaScript throws TypeError.
Compared with replace(/pat/g, …), replaceAll("pat", …) is safer for literal text from users because characters like . and * are not treated as regex metacharacters.
💡
Beginner tip
replace("a", "b") → first "a" only. replaceAll("a", "b") → every "a".
Examples follow MDN String.replaceAll() patterns. Use View Output or Try It Yourself for each case.
📚 Getting Started
Replace every match with a string or a global regex.
Example 1 — Basic replaceAll()
MDN demo: replace every "name", then every word-boundary is.
JavaScript
const paragraph = "This dog's name is just Dog! Yes, that is the name.";
paragraph.replaceAll("name", "nickname");
// "This dog's nickname is just Dog! Yes, that is the nickname."
paragraph.replaceAll(/\bis\b/g, "was");
// "This dog's name was just Dog! Yes, that was the name."
This dog's nickname is just Dog! Yes, that is the nickname.
This dog's name was just Dog! Yes, that was the name.
How It Works
Both "name" occurrences become "nickname". The global regex swaps both whole-word is matches for was.
Example 2 — Replace Every Character Match
MDN walkthrough: swap every "b" for a dot.
JavaScript
"aabbcc".replaceAll("b", ".");
// "aa..cc"
"aabbcc".replaceAll(/b/g, ".");
// "aa..cc"
"I love cats and cats!".replaceAll("cats", "dogs");
// "I love dogs and dogs!"
Empty matches exist between characters (and at the ends). replaceAll fills all of them; replace only fills the start.
Example 5 — Safer Literal Redaction
MDN idea: string replaceAll avoids regex surprises in names.
JavaScript
function unsafeRedact(text, name) {
return text.replace(new RegExp(name, "g"), "[REDACTED]");
}
function safeRedact(text, name) {
return text.replaceAll(name, "[REDACTED]");
}
const report =
"A hacker called ha.*er used special characters in their name.";
unsafeRedact(report, "ha.*er");
// "A [REDACTED]s in their name." (.* ate too much!)
safeRedact(report, "ha.*er");
// "A hacker called [REDACTED] used special characters in their name."
A [REDACTED]s in their name.
A hacker called [REDACTED] used special characters in their name.
How It Works
Building new RegExp(name, "g") treats .* as “any characters”. replaceAll(name, …) treats the name as plain text.
Applications
🚀 Common Use Cases
Bulk find-and-replace — swap every copy of a word or token.
Cleaning user text — remove or mask repeated substrings safely.
Template cleanup — replace placeholders that appear many times.
Global regex transforms — with an explicit g flag.
Not for first-only edits — use replace() when one swap is enough.
Not for reading matches — use match() / matchAll() to inspect hits.
🧠 How replaceAll() Builds the Result
1
Read pattern + replacement
Accept a string/RegExp pattern and a string or function replacement.
Input
2
Validate regex
If the pattern is a RegExp, require the g flag or throw TypeError.
Guard
3
Replace every match
Walk all matches and apply the replacement string or function.
Swap
4
✅
Return a new string
The original str is unchanged.
Important
📝 Notes
replaceAll with a string pattern replaces every occurrence.
A regex argument must include the g flag or a TypeError is thrown.
Empty string pattern inserts between every UTF-16 code unit.
Prefer string replaceAll for literal text that might contain regex characters.
Replacement strings support the same $ patterns as replace().
Always capture the return value — the original string does not update in place.
Compatibility
Browser & Runtime Support
String.prototype.replaceAll() is Baseline Widely available — supported across modern browsers since August 2020 (ES2021).
✓ Baseline · Widely available
String.replaceAll()
Safe for modern production targets in browsers and runtimes (Node.js, Deno, Bun). Older engines may need a polyfill.
ModernWidely available
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
replaceAll()Excellent (modern)
Bottom line: Use String.replaceAll() when you want every match replaced. Remember: regex patterns require the g flag, and empty-string patterns insert between every character.
Wrap Up
Conclusion
String.prototype.replaceAll() makes “change every occurrence” obvious — especially for plain string patterns. Use a global regex when you need pattern power, and keep replace() handy for first-only swaps.
Pass a string pattern for literal user-controlled text
Include the g flag on every regex you pass in
Prefer replace when you only need the first match
Assign the return value to a new variable
❌ Don’t
Pass a non-global regex to replaceAll
Build new RegExp(userText, "g") without escaping
Forget empty-string patterns insert between characters
Expect the original string to change in place
Use replaceAll when a single swap is enough
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about String.replaceAll()
Replace every match — clearly and safely for literals.
5
Core concepts
📝01
Returns
new string
API
🔎02
String pattern
all matches
Rule
❌03
Regex needs
g flag
Guard
⚡04
vs replace
all vs first
Compare
📄05
Mutates
no
Immutable
❓ Frequently Asked Questions
String.prototype.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, or when you already rely on replace’s first-match behavior.
Yes for literal text. Passing a string to replaceAll avoids accidentally treating characters like . * + as regex metacharacters (which can happen if you build new RegExp(userText, "g")).
An empty string pattern inserts the replacement between every UTF-16 code unit. For example "xxx".replaceAll("", "_") returns "_x_x_x_".
Did you know?
replaceAll() landed in ES2021 after years of developers writing split(old).join(new) or forgetting the g flag on replace(). The new method makes “all matches” the default intent.