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.

🌐
Alexanderkaran
blog.alexanderkaran.com › replace-vs-replace-all
Replace vs Replace All Comparison - Alexander Karan's Blog
October 19, 2024 - Learn how `replace` and `replaceAll` functions differ, and discover the benefits of using `replaceAll` for global string replacements
Discussions

Using replace instead of replaceAll
The String#replace method matches and replaces substrings literally. It doesn't accept a RegEx, so you don't need to escape the String with backslashes. Check the documentation for these methods: https://docs.oracle.com/javase/7/docs/api/java/lang/String.html Would X work here? Just try it out. You can use Java's jshell tool instead of creating a file for your testing code. To test RegExs you can use a site like https://regex101.com/ , just remember that you need to escape backslashes again when you copy a RegEx from this site into a Java String. Your IDE might have a setting to automatically escape pasted strings for you. More on reddit.com
🌐 r/javahelp
2
3
November 8, 2022
Best way to find and replace all instances if a string in an array of strings?
Currently I am doing this by iterating through each of the arrays and checking each value for the replaceable values and replacing the matches. That's pretty much what you'd need to do. map() is the method used for accomplishing this var arr = ["826", "7161", "", "", "x", "927", "hah", "hg7)", "x"] var arrWithClosed = arr.map(str => !str || str === "x" ? "Closed" : str) console.log(arrWithClosed) // 826, 7161, Closed, Closed, Closed, 927, hah, hg7), Closed More on reddit.com
🌐 r/learnjavascript
8
5
January 6, 2023
Suitelet replaceAll() function
You can use the global flag g in your replace. "S t r i n g".replace(/ /g, "") => "String" mdn docs More on reddit.com
🌐 r/Netsuite
8
4
December 1, 2023
time complexity of javascript functions
Any toString method where the returned string contains the object's contents in some form obviously can't be O(1) (you can't create a string containing n items in less than O(n) time). A toString method that just returns a fixed string (like Object.prototype.toString, which just returns "[object Object]") would be O(1) though. String.prototype.replace has to at least be O(n) because it has to search through the entire string in the worst place and because it has to create a new string whose length will linear in the length of the original string. And creating a string of length n is of course an O(n) operation. But with a sufficiently complicated regex, the complexity can actually be much worse than O(n) - the complexity of matching regular expressions, as implemented in JavaScript and many other languages at least, can be exponential in pathological cases. More on reddit.com
🌐 r/learnjavascript
7
1
July 18, 2019
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › difference-between-stringprototypereplace-and-stringprototypereplaceall-in-javascript
Difference Between String.prototype.replace() and String.prototype.replaceAll() in JavaScript - GeeksforGeeks
July 23, 2025 - Both String.prototype.replace and ... in JavaScript. The replace is versatile for the various replacement needs while replaceAll offers a straightforward approach for the replacing all instances of a pattern....
🌐
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.
🌐
DEV Community
dev.to › vladymir01 › using-replace-and-replaceall-in-javascript-102e
Using replace() and replaceAll() in JavaScript - DEV Community
October 17, 2021 - #javascript #webdev #beginners #string · 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(). The replace() method can be used to search from a string, a specific character or a substring that matches a pattern that you provide in order to replace it by another character or a new substring.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replaceAll
String.prototype.replaceAll() - JavaScript - MDN Web Docs
The replaceAll() method of String values returns a new string with all matches of a pattern replaced by a replacement. The pattern can be a string or a RegExp, and the replacement can be a string or a function to be called for each match. The original string is left unchanged.
Find elsewhere
🌐
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.
🌐
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.
🌐
Dmitri Pavlutin
dmitripavlutin.com › replace-all-string-occurrences-javascript
3 Ways To Replace All String Occurrences in JavaScript
January 27, 2023 - If search argument is a string, replaceAll() replaces all occurrences of search with replaceWith, while replace() replaces only the first occurence · If search argument is a non-global regular expression, then replaceAll() throws a TypeError ...
🌐
Attacomsian
attacomsian.com › blog › javascript-string-replace
How to use String replace() method in JavaScript
October 23, 2022 - A quick introduction to the JavaScript string replace() and replaceAll() methods and how to use them to replace a text in a string.
🌐
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(",", ...
🌐
MeasureThat
measurethat.net › Benchmarks › Show › 2396 › 0 › replaceall-vs-regex-replace
Benchmark: replaceAll vs regex replace - MeasureThat.net
JavaScript benchmarks, JavaScript performance playground. Measure performance accross different browsers. javascript benchmarks online.
🌐
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....
🌐
Christiankohler
christiankohler.net › it-s-a-trap-the-biggest-pitfall-of-string-prototype-replace
It’s a trap - The biggest pitfall of String.prototype.replace()
June 30, 2020 - Always use replaceAll if your environment supports it. You can use it with the string pattern or the regex pattern and you avoid the pitfall of replacing only the first occurrence.
🌐
CodeShack
codeshack.io › home › references › javascript › string.replaceall()
JavaScript string.replaceAll() Method: Syntax & Examples
June 23, 2026 - JavaScript string.replaceAll() replaces every occurrence of a substring. Learn how it differs from replace(), the regex rule, and browser support.
🌐
JavaScript Tutorial
javascripttutorial.net › home › javascript string methods › string.prototype.replaceall()
JavaScript String replaceAll() Method
November 4, 2024 - Like the replace() method, the replaceAll() method doesn’t change the original string but returns a completely new string with the pattern replaced by the replacement. Note that the replaceAll() method is available in ES2021 or later. Let’s take some examples of using the JavaScript String replaceAll() method.
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › replaceall
String replaceAll() in JavaScript - Mastering JS
March 21, 2022 - As of 2022, we do not recommend using replaceAll() due to limited support. Use String.prototype.replace() with a regular expression instead. const sentence = 'The world is a cruel place.'; sentence.replace(/cruel/g, 'wonderful'); // The world is a wonderful place. You can use a regular expression in place of a string if you want to cover more cases of what needs to be replaced. It is important to not that your regular expression must have the g flag enabled. If not, JavaScript will throw a TypeError.
🌐
Medium
medium.com › geekculture › replaceall-in-javascript-b61f4e94f028
replaceAll in JavaScript
June 28, 2021 - replaceAll in JavaScript String.prototype.replaceAll() replaces all occurrence of a string with another string value. Syntax: const newStr = str.replaceAll(regexp|substr, newSubstr|function) There …
🌐
MUI Stack
muhimasri.com › blogs › how-to-replace-multiple-words-and-characters-in-javascript
How to Replace Multiple Words and Characters in JavaScript
December 6, 2021 - The main difference is that replace() replaces the first occurrence while replaceAll() replaces all occurrences of the search. For example: const p = 'The quick brown fox jumps over the lazy dog.