regexAllGlobalFlags
Reports
matchAll()andreplaceAll()calls with regex arguments missing the global flag.
✅ This rule is included in the ts logical presets.
When calling String.prototype.matchAll() or String.prototype.replaceAll() with a regular expression, the regex must have the global (g) flag.
Without it, JavaScript throws a TypeError at runtime.
This rule detects regex literals and RegExp constructor calls that are missing the global flag when passed to these methods.
Examples
Section titled “Examples”"hello world".String.matchAll(regexp: RegExp): RegExpStringIterator<RegExpExecArray>
Matches a string with a regular expression, and returns an iterable of matches
containing the results of that search.
matchAll(/o/);"hello world".String.replaceAll(searchValue: string | RegExp, replaceValue: string): string (+1 overload)
Replace all instances of a substring in a string, using a regular expression or search string.
replaceAll(/o/, "0");const const text: "example"
text = "example";const text: "example"
text.String.matchAll(regexp: RegExp): RegExpStringIterator<RegExpExecArray>
Matches a string with a regular expression, and returns an iterable of matches
containing the results of that search.
matchAll(new var RegExp: RegExpConstructornew (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp("e"));"hello world".String.matchAll(regexp: RegExp): RegExpStringIterator<RegExpExecArray>
Matches a string with a regular expression, and returns an iterable of matches
containing the results of that search.
matchAll(/o/g);"hello world".String.replaceAll(searchValue: string | RegExp, replaceValue: string): string (+1 overload)
Replace all instances of a substring in a string, using a regular expression or search string.
replaceAll(/o/g, "0");const const text: "example"
text = "example";const text: "example"
text.String.matchAll(regexp: RegExp): RegExpStringIterator<RegExpExecArray>
Matches a string with a regular expression, and returns an iterable of matches
containing the results of that search.
matchAll(new var RegExp: RegExpConstructornew (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp("e", "g"));"hello world".String.replaceAll(searchValue: string | RegExp, replaceValue: string): string (+1 overload)
Replace all instances of a substring in a string, using a regular expression or search string.
replaceAll("o", "0");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you’re transpiling to an environment without matchAll() or replaceAll() support and have polyfills that don’t enforce the global flag requirement, you might want to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-missing-g-flag - Oxlint:
oxc/bad-replace-all-arg