Get to know MDN better
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.
The String.prototype.matchAll() and String.prototype.replaceAll() methods require a RegExp object with the global flag set. This flag indicates that the regular expression can match all locations of the input string, instead of stopping at the first match. Although the g flag is redundant when using these methods (because these methods always do a global replacement), they are still required to make the intention clear.
It's worth noting that the g flag validation is done in the matchAll and replaceAll methods. If you use the [Symbol.matchAll]() method of RegExp instead, you won't get this error, but there will only be a single match.
If you intend to do global matching/replacement: either add the g flag, or construct a new RegExp object with the g flag, if you want to keep the original regex unchanged.
If you only intend to do a single matching/replacement: use String.prototype.match() or String.prototype.replace() instead. You can also use the [Symbol.matchAll]() method if you want an iterator like matchAll returns that only contains one match, but doing so will be very confusing.
This page was last modified on Jul 8, 2025 by MDN contributors.
Your blueprint for a better internet.
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998–2026 by individual mozilla.org contributors. Content available under a Creative Commons license.