🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Errors › Requires_global_RegExp
TypeError: matchAll/replaceAll must be called with a global RegExp - JavaScript | MDN
July 8, 2025 - The JavaScript exception "TypeError: matchAll/replaceAll must be called with a global RegExp" occurs when the String.prototype.matchAll() or String.prototype.replaceAll() method is used with a RegExp object that does not have the global flag set. TypeError: String.prototype.matchAll called ...
🌐
DeepScan
deepscan.io › docs › rules › bad-replace-all-arg
String.prototype.replaceAll() should not be called with a ...
This rule applies when a regular expression without the global flag (g) is used at String.prototype.replaceAll().
🌐
Designcise
designcise.com › web › tutorial › how-to-fix-string-prototype-replaceall-called-with-a-non-global-regexp-argument-javascript-error
How to Fix "replaceAll called with a non-global RegExp argument" JavaScript Error? - Designcise
January 4, 2024 - When you use a regular expression ... // ... The reason this happens is that the global flag is required when using a regular expression with String.prototype.replaceAll(). Omitting the global flag results in a TypeError, ...
🌐
GitHub
github.com › eslint › eslint › issues › 15073
New Rule: require global regular expression to be used with `replaceAll` · Issue #15073 · eslint/eslint
September 16, 2021 - This rule should require that regular expressions passed as the first argument to replaceAll are global. String.prototype.replaceAll: https://github.com/tc39/proposal-string-replaceall · Warns about a potential problem · `abc`.replaceAll(/a/, ...
Author: eslint
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › String › replaceAll
String.prototype.replaceAll() - JavaScript - MDN Web Docs
The replacement has the same semantics as that of String.prototype.replace(). A new string, with all matches of a pattern replaced by a replacement. ... Thrown if the pattern is a regex that does not have the global (g) flag set (its flags property does not contain "g").
🌐
GitHub
github.com › tc39 › proposal-string-replaceall › issues › 16
How should replaceAll behave if searchValue is a non-global RegExp? · Issue #16 · tc39/proposal-string-replaceall
March 21, 2019 - Related to, but different from #8. #8 wants it to auto-convert a non-global regex to a global regex. I think we should accept regex searchValues, but not convert them to a global regex. String.p.matchAll is setting precedent for "all" me...
Author: tc39
🌐
Mastering JS
masteringjs.io › tutorials › fundamentals › replaceall
String replaceAll() in JavaScript - Mastering JS
March 21, 2022 - Only cruel people thrive here.'; sentence.replaceAll(/cruel/ig, 'wonderful'); // The world is a wonderful place. Only wonderful people thrive here. // TypeError: String.prototype.replaceAll called with a non-global RegExp argument sentence.replaceAll(/cruel/i, 'wonderful');
🌐
Medium
medium.com › nerd-for-tech › basics-of-javascript-string-replaceall-method-e53b0ce22a92
Basics of Javascript · String · replaceAll() (method) | by Jakub Korch | Medium
June 17, 2021 - try { let re = /(\w+)\s(\w+)/; let fullName = 'John Smith'; let newstr = fullName.replaceAll(re, '$2, $1'); console.log(newstr); // Smith, John } catch(err){ console.log(err); }// OUTPUT: // TypeError: String.prototype.replaceAll called with a non-global // RegExp argument at String.replaceAll (<anonymous>)try { let re = /(\w+)\s(\w+)/g; let fullName = 'John Smith'; let newstr = fullName.replaceAll(re, '$2, $1'); console.log(newstr); // Smith, John } catch(err){ console.log(err); }// OUTPUT: // Smith, John
🌐
Medium
medium.com › geekculture › replaceall-in-javascript-b61f4e94f028
replaceAll in JavaScript
June 28, 2021 - The Mathias bynens proposal solves these problems and gives a very easy way to do substring replacement using `replaceAll()` which replaces all instances of a substring in a string with another string value without using a global regexp.
Find elsewhere
🌐
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 - const my_string = "I like dogs because dogs are adorable!"; let pattern = /dogs/; let replacement = "cats"; let my_new_string = my_string.replaceAll(pattern,replacement); console.log(my_new_string); // output // test.js:6 Uncaught TypeError: String.prototype.replaceAll called with a // non-global RegExp argument // at String.replaceAll (<anonymous>) // at test.js:6:31 ·
🌐
RPG Maker Forums
forums.rpgmakerweb.com › home › game development engines › legacy engine support › rpg maker mv support
rpg maker mv - TypeError: String.prototype.replaceAll called with a non-global RegExp argument | RPG Maker Forums
April 17, 2022 - String.prototype.replaceAll = function (search_string, replace_string) { console.log("String.replaceAll made by RETRO"); return this.split(search_string).join(replace_string) }; (save changes >> deploy (make sure retro.js isn't loaded from cache). Then you can check console log if that polyfill is used for String.replaceAll If "String.replaceAll made by RETRO" won't appear in log it means something is overriding that method.
🌐
Proposals
proposals.es › proposals › String.prototype.replaceAll
String.prototype.replaceAll proposal
Currently there is no way to replace all instances of a substring in a string without use of a global regexp. String.prototype.replace only affects the first occurrence when used with a string argument.
Top answer
1 of 16
5258

As of August 2020: Modern browsers have support for the String.replaceAll() method defined by the ECMAScript 2021 language specification.


For older/legacy browsers:

function escapeRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}

Here is how this answer evolved:

str = str.replace(/abc/g, '');

In response to comment "what's if 'abc' is passed as a variable?":

var find = 'abc';
var re = new RegExp(find, 'g');

str = str.replace(re, '');

In response to Click Upvote's comment, you could simplify it even more:

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(find, 'g'), replace);
}

Note: Regular expressions contain special (meta) characters, and as such it is dangerous to blindly pass an argument in the find function above without pre-processing it to escape those characters. This is covered in the Mozilla Developer Network's JavaScript Guide on Regular Expressions, where they present the following utility function (which has changed at least twice since this answer was originally written, so make sure to check the MDN site for potential updates):

function escapeRegExp(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

So in order to make the replaceAll() function above safer, it could be modified to the following if you also include escapeRegExp:

function replaceAll(str, find, replace) {
  return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
2 of 16
2537

For the sake of completeness, I got to thinking about which method I should use to do this. There are basically two ways to do this as suggested by the other answers on this page.

Note: In general, extending the built-in prototypes in JavaScript is generally not recommended. I am providing as extensions on the String prototype simply for purposes of illustration, showing different implementations of a hypothetical standard method on the String built-in prototype.


Regular Expression Based Implementation

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.replace(new RegExp(search, 'g'), replacement);
};

Split and Join (Functional) Implementation

String.prototype.replaceAll = function(search, replacement) {
    var target = this;
    return target.split(search).join(replacement);
};

Not knowing too much about how regular expressions work behind the scenes in terms of efficiency, I tended to lean toward the split and join implementation in the past without thinking about performance. When I did wonder which was more efficient, and by what margin, I used it as an excuse to find out.

On my Chrome Windows 8 machine, the regular expression based implementation is the fastest, with the split and join implementation being 53% slower. Meaning the regular expressions are twice as fast for the lorem ipsum input I used.

Check out this benchmark running these two implementations against each other.


As noted in the comment below by @ThomasLeduc and others, there could be an issue with the regular expression-based implementation if search contains certain characters which are reserved as special characters in regular expressions. The implementation assumes that the caller will escape the string beforehand or will only pass strings that are without the characters in the table in Regular Expressions (MDN).

MDN also provides an implementation to escape our strings. It would be nice if this was also standardized as RegExp.escape(str), but alas, it does not exist:

function escapeRegExp(str) {
  return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
}

We could call escapeRegExp within our String.prototype.replaceAll implementation, however, I'm not sure how much this will affect the performance (potentially even for strings for which the escape is not needed, like all alphanumeric strings).

🌐
V8
v8.dev › features › string-replaceall
String.prototype.replaceAll · V8
November 11, 2019 - For consistency with the pre-existing ... while String#replaceAll replaces all occurrences. If searchValue is a non-global RegExp, then String#replace replaces only a single match, similar to how it behaves for strings....
🌐
RPG Maker Forums
forums.rpgmakerweb.com › home
RPG Maker Forums
April 17, 2022 - Post here if you have specific ideas or a game prototype that you want people to try out!
🌐
DeepScan
deepscan.io › docs › rules › bad-match-all-arg
String.prototype.matchAll() should be called with a global ...
This rule applies when a regular expression without the global flag (g) is used at String.prototype.matchAll().
🌐
GitHub
github.com › mdn › content › blob › main › files › en-us › web › javascript › reference › errors › requires_global_regexp › index.md
content/files/en-us/web/javascript/reference/errors/requires_global_regexp/index.md at main · mdn/content
The JavaScript exception "TypeError: ... RegExp" occurs when the {{jsxref("String.prototype.matchAll()")}} or {{jsxref("String.prototype.replaceAll()")}} method is used with a {{jsxref("RegExp")}} object that does not have the {{jsxref("RegExp/global", "global")}} flag set...
Author: mdn
🌐
Xah Lee
xahlee.info › js › js_String.prototype.matchAll.html
JS: String.prototype.matchAll
If the argument is a regex object, it must have the regex flag g, else its error. // the regex object should have flag g // "year 1999".matchAll(/\d{4}/) // error: Uncaught TypeError: String.prototype.matchAll called with a non-global RegExp argument
🌐
WebKit
bugs.webkit.org › show_bug.cgi
202471 – Implement String.prototype.replaceAll
WebKit Bugzilla · Browse · Search+ · Log In · Top of Page · Format For Printing · Clone This Bug · Reports