From gleaning the documentation for replaceAll, we find the following tidbits:

const newStr = str.replaceAll(regexp|substr, newSubstr|function)

Note: When using a regexp you have to set the global ("g") flag; otherwise, it will throw a TypeError: "replaceAll must be called with a global RegExp".

In other words, when calling replaceAll with a regex literal or RegExp, it must use the global flag. So, there doesn't seem to be much gained by calling replaceAll versus just using the current replace. However, one difference with replaceAll is that when passing it a string, it will automatically do a global replacement. This is where you might save yourself a bit of typing, by not having to enter a global flag.

Answer from Tim Biegeleisen on Stack Overflow
Top answer
1 of 2
48

From gleaning the documentation for replaceAll, we find the following tidbits:

const newStr = str.replaceAll(regexp|substr, newSubstr|function)

Note: When using a regexp you have to set the global ("g") flag; otherwise, it will throw a TypeError: "replaceAll must be called with a global RegExp".

In other words, when calling replaceAll with a regex literal or RegExp, it must use the global flag. So, there doesn't seem to be much gained by calling replaceAll versus just using the current replace. However, one difference with replaceAll is that when passing it a string, it will automatically do a global replacement. This is where you might save yourself a bit of typing, by not having to enter a global flag.

2 of 2
1

There are performance and memory footprint differences between those methods. This might be important in low-level operations that are executed many times on your web page (such as general replacements done on page load). You can get away using replace function most of the time, but never use split/join.

Split-join

Split-join is very inefficient; never use it for replacements. It performs a search, creates an array (possibly very large), and then loops over the array to build a new string. Replacements should simply construct a new string directly.

// ❌ don't do this
console.log(source.split(str1).join(str2));

replaceAll(string) vs replace

Use replaceAll with a string when you can. The call to replaceAll(string, string) will be significantly faster than using a RegExp, as it's easier to optimize. Benchmarks show replaceAll is about 2x faster in Firefox 147 (even for very short strings). Also note that there is no point in compiling regular expressions when you only need to work with strings (new RegExp adds both object creation and parser overhead).

// ✅replaceAll(str) wins over ❌replace(re)
console.log(source.replace(new RegExp(str1,"g"), str2));
//versus
console.log(source.replaceAll(str1, str2));

On top of performance issues, if str1 contains special characters, you might get very unusual results:

// ❌ this will just be wrong; all this is part of a regexp: `[/, '').replace(/]`
'ab[c]'.replace(/[/, '').replace(/]/, ''); // -> b[c]
// ❌ `[` should be escaped and so you get: SyntaxError: unterminated character class.
'ab[c]'.replace(new RegExp('['), '').replace(new RegExp(']'), '')

replaceAll(RegExp) vs replace

When you want to use a regular expression, there is less of a difference between replaceAll and replace. In terms of performance, replace might actually be better, as it has been optimized over a longer period of time in browsers.

// replaceAll(re) ~= replace(re)
console.log(source.replace(regex, replacement));
//versus
console.log(source.replaceAll(regex, replacement));

Note however that it might be a good thing that 'abc'.replaceAll(/abc/, '') throws an error. If your intention is to replace all occurrences, it's very easy to make a mistake and forget the g modifier. So replace might fail silently, but replaceAll will let you know.

/** Replaces all occurrences of "ice" with "summer" */
function replaceIceWithSummer(text) {
    // ❌oh no we forgot g, ✅but replaceAll will remind us to replace all of ice
    return text.replaceAll(/ice/i, "summer");
}

That is even more important when the regexp is used in many places.

🌐
javaspring
javaspring.net › blog › javascript-string-replace-vs-replaceall
JavaScript String `replace` vs `replaceAll`: Key Differences in ECMAScript 2021 Explained — javaspring.net
Use replaceAll() when you need to replace all occurrences explicitly. It’s safer with regex (enforces g flag) and more readable for "replace all" intent, especially with string patterns.
People also ask

What is the difference between replace and replaceAll?
replace with a string replaces the first occurrence; replaceAll replaces all of them. With a regex both behave the same, except replaceAll throws a TypeError if the pattern lacks the g flag.
🌐
runxbuild.com
runxbuild.com › home › blog › javascript replace(): the first-match gotcha and what replaceall fixed
JavaScript replace(): The First-Match Gotcha and What replaceAll Fixed
Why did my replace not change the string?
Strings are immutable, so replace returns a new string rather than modifying the original. The result must be assigned — a discarded return value produces no error and no effect.
🌐
runxbuild.com
runxbuild.com › home › blog › javascript replace(): the first-match gotcha and what replaceall fixed
JavaScript replace(): The First-Match Gotcha and What replaceAll Fixed
Why does replace only replace the first match?
Because a string pattern always matches once. Use a regular expression with the g flag, or replaceAll, which replaces every occurrence and is clearer about the intent.
🌐
runxbuild.com
runxbuild.com › home › blog › javascript replace(): the first-match gotcha and what replaceall fixed
JavaScript replace(): The First-Match Gotcha and What replaceAll Fixed
🌐
W3Schools
w3schools.com › jsref › jsref_string_replaceall.asp
JavaScript String replaceAll() Method
The replaceAll() method does not change the original string. The replaceAll() method was introduced in JavaScript 2021. If the parameter is a regular expression, the global flag (g) must be set, otherwise a TypeError is thrown.
🌐
DEV Community
dev.to › vladymir01 › using-replace-and-replaceall-in-javascript-102e
Using replace() and replaceAll() in JavaScript - DEV Community
October 17, 2021 - Posted on Oct 17, 2021 · #javascript #string #beginners #webdev · In this tutorial, we're going to see how to use the methods replace() and replaceAll() in javascript. Both methods are part of the String object. that means you can invoke them on strings. Let's start with replace().
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replaceAll
String.prototype.replaceAll() - JavaScript - MDN Web Docs
In this case the behavior of replaceAll() is entirely encoded by the [Symbol.replace]() method, and therefore will have the same result as replace() (apart from the extra input validation that the regex is global). If the pattern is an empty string, the replacement will be inserted in between every UTF-16 code unit, similar to split() behavior.
🌐
Alexanderkaran
blog.alexanderkaran.com › replace-vs-replace-all
Replace vs Replace All Comparison - Alexander Karan's Blog
October 19, 2024 - The fantastic function replaceAll has full support across all browsers except IE; you can find the full breakdown here. ... After posting on BlueSky, Larry Williamson pointed out another method of replacing all occurrences in a string.
🌐
Saeloun Blog
blog.saeloun.com › 2021 › 08 › 27 › es2021-replace-all-numeric-separator
ES2021 replaceAll and Numeric Separators | Saeloun Blog
August 26, 2021 - String.prototype.replaceAll(subString, newSubstring) subString: Input string that is to be replaced by a new value. It can be a string or a regular expression. newSubstring: The string that replaces the substring argument · The original string is left unchanged and a new string is returned. Before ECMAScript 2021 one of the most common ways of doing this was to use a global regexp.
Find elsewhere
🌐
Medium
medium.com › geekculture › replaceall-in-javascript-b61f4e94f028
replaceAll in JavaScript
June 28, 2021 - replaceAll in Javascript · JavaScript ... 2021 · 211 · 1 · Listen · Share · String.prototype.replaceAll() replaces all occurrence of a string with another string value....
🌐
RunxBuild
runxbuild.com › home › blog › javascript replace(): the first-match gotcha and what replaceall fixed
JavaScript replace(): The First-Match Gotcha and What replaceAll Fixed
August 8, 2026 - What is the difference between replace and replaceAll? replace with a string replaces the first occurrence; replaceAll replaces all of them.
🌐
David Walsh
davidwalsh.name › javascript-s
JavaScript String replaceAll
December 27, 2021 - JavaScript String replaceAll · Building Resilient Systems on AWS: Learn how to design and implement a resilient, highly available, fault-tolerant infrastructure on AWS. By David Walsh on December 27, 2021 · Replacing a substring of text within a larger string has always been misleading in JavaScript.
🌐
V8
v8.dev › features › string-replaceall
String.prototype.replaceAll · V8
November 11, 2019 - If searchValue is a string, then String#replace only replaces the first occurrence of the substring, while String#replaceAll replaces all occurrences.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replace
String.prototype.replace() - JavaScript - MDN Web Docs
A string pattern will only be replaced once. To perform a global search and replace, use a regular expression with the g flag, or use replaceAll() instead.
🌐
CodeShack
codeshack.io › home › references › javascript › string.replaceall()
JavaScript string.replaceAll() Method: Syntax & Examples
June 23, 2026 - replaceAll() exists to fix the most common surprise in JavaScript strings: that replace() with a plain string only changes the first match.
🌐
SDKLABS
javascript.ac › en › reference › string-replaceall
JavaScript.ac - Learn JavaScript & Go
Assuming the replacement string is inserted verbatim: the $-patterns of replace() still apply, so $$ collapses to a single dollar sign and $& reinserts the match. Replacing into prices, or splicing in user-provided replacement text, can corrupt output; escape dollars or use a function replacer. Forgetting environment support: replaceAll() is ES2021, so very old browsers and Node versions before 15 lack it, and calling it there throws at runtime.
🌐
TutorialsPoint
tutorialspoint.com › javascript › string_replace.htm
JavaScript String replace() Method
The replaceAll() method replaces all occurrences of a search value or a regex with a specified replacement, for example: "_tutorials_point_".replaceAll("_", "") returns "tutorialspoint", whereas the replace() method replaces only the first ...
🌐
freeCodeCamp
freecodecamp.org › news › javascript-replaceall-replace-all-instances-of-a-string-in-js
JavaScript replaceAll() – Replace All Instances of a String in JS
July 28, 2022 - If pattern is a regular expression, ... specifically, the error will be a TypeError. replacement is the second parameter, which can be another string or a function to replace pattern....
🌐
TutorialsPoint
tutorialspoint.com › javascript › javascript_string_replaceall_method.htm
JavaScript String replaceAll() Method
Following is the difference between the replace() and replaceAll() method − · The replace() method replaces only the first occurrence of a search value or a regex with a specified replacement, for example: ",tutorials,point,".replace(",", ...
🌐
Medium
medium.com › @python-javascript-php-html-css › mastering-string-replacement-in-javascript-41dc9e39665e
Learning JavaScript String Replacement | by Denis Bélanger
August 24, 2024 - Additionally, JavaScript’s newer methods, like .replaceAll(), introduced in ECMAScript 2021, offer a more straightforward syntax for achieving the same result without requiring a regular expression for simple replacements.
🌐
Flavio Copes
flaviocopes.com › home › javascript › how to replace all occurrences of a string in javascript
How to replace all occurrences of a string in JavaScript
July 2, 2018 - Since we pass a plain string to split(), there’s no regex involved, and no escaping problem with special characters. JavaScript now has a dedicated method for this, replaceAll(), added in ES2021:
🌐
Attacomsian
attacomsian.com › blog › javascript-string-replace
How to use String replace() method in JavaScript
October 23, 2022 - The first parameter can be a string or a regular expression. If it is a string value, only the first instance of the value will be replaced.