pageClass | sidebarDepth | title | description | since |
---|---|---|---|---|
rule-details |
0 |
regexp/no-missing-g-flag |
disallow missing `g` flag in patterns used in `String#matchAll` and `String#replaceAll` |
v1.10.0 |
💼 This rule is enabled in the ✅ plugin:regexp/recommended
config.
🔧 This rule is automatically fixable by the --fix
CLI option.
disallow missing
g
flag in patterns used inString#matchAll
andString#replaceAll
When calling String#matchAll()
and String#replaceAll()
with a RegExp
missing the global (g
) flag, it will be a runtime error.
This rule reports RegExp
s missing the global (g
) flag that cause these errors.
/* eslint regexp/no-missing-g-flag: "error" */
const games = 'table football, foosball'
const text = 'The quick brown fox jumps over the lazy dog. If the dog reacted, was it really lazy?';
/* ✓ GOOD */
var m = games.matchAll(/foo/g);
var newText = text.replaceAll(/Dog/ig, 'cat');
/* ✗ BAD */
var m = games.matchAll(/foo/);
var newText = text.replaceAll(/Dog/i, 'cat');
{
"regexp/no-missing-g-flag": ["error",
{
"strictTypes": true
}
]
}
strictTypes
... Iftrue
, strictly check the type of object to determine if the regex instance was used inmatchAll()
andreplaceAll()
. Default istrue
.
This option is always on when using TypeScript.
This rule was introduced in eslint-plugin-regexp v1.10.0