JavaScript String replaceAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

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)
CallWhat 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

ParameterDescription
patternRequired. Substring, or RegExp with g
replacementRequired. New text, or a function returning text

Return Value

Return ValueDescription
*The value returned by string.replaceAll().

JavaScript Examples

Example 1 — Replace Every Literal Match

A string pattern swaps every occurrence — no regex required.

JavaScript
const text = "hello world, world";

console.log(text.replaceAll("world", "JS"));
// "hello JS, JS"

console.log("aabbcc".replaceAll("b", "."));
// "aa..cc"
Try It Yourself

How It Works

  1. Pass a plain string. "world" is treated as literal text.
  2. Swap every hit. Both "world" occurrences become "JS".
  3. Works for characters too. Every "b" in "aabbcc" becomes ".".
  4. 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"
Try It Yourself

How It Works

  1. String + replaceAll. Every "a" becomes "b".
  2. String + replace. Only the first "a" changes.
  3. Regex with g. Both APIs replace every match when g is set.
  4. Rule of thumb. Want all literals? Prefer replaceAll — clearer than remembering /g.

Example 3 — RegExp Must Include g

A non-global regex throws; add g (and i if needed).

JavaScript
try {
  "aabbcc".replaceAll(/b/, ".");
} catch (e) {
  console.log(e.name); // TypeError
}

console.log("aabbcc".replaceAll(/b/g, "."));
// "aa..cc"

console.log("Apples and apples".replaceAll(/apples/gi, "oranges"));
// "oranges and oranges"
Try It Yourself

How It Works

  1. Require global. Without g, replaceAll throws TypeError.
  2. Add the flag. /b/g replaces every b.
  3. Combine with i. /apples/gi matches any capitalization.
  4. Or skip regex. For plain text, a string pattern is simpler and safer.

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 Chrome Supported · Desktop & Mobile
Full support
Mozilla Firefox Supported · Desktop & Mobile
Full support
Apple Safari Supported · macOS & iOS
Full support
Microsoft Edge Supported · Chromium
Full support
Internet Explorer No native support · Use a polyfill
Polyfill
Opera Supported · Modern versions
Full support
Samsung Internet Supported · Android
Full support
Bun Supported · JavaScript runtime
Supported
Deno Supported · JavaScript runtime
Supported
Node.js Supported · Server runtime
Supported
Android WebView Supported · 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_".

Next: search()

Learn how to find the index of the first regex match in a string.

search() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful