Skip to content

useGlobalRegexInMatchAll (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useGlobalRegexInMatchAll": "error"
}
}
}
}

Require regular expressions passed to matchAll() to have the g flag.

String#matchAll() finds every match of a regular expression in a string. It only works with regular expressions that have the g (global) flag, such as /pattern/g. When it receives a regular expression without this flag, it throws a TypeError instead of returning the matches.

This rule checks regular expression literals, such as /pattern/, and regular expressions created with new RegExp() or RegExp() when their flags are written as a string. It also follows const variables to the regular expression they hold.

text.matchAll(/pattern/);
code-block.js:1:15 lint/nursery/useGlobalRegexInMatchAll ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The regular expression passed to matchAll() is missing the g flag.

> 1 │ text.matchAll(/pattern/);
│ ^^^^^^^^^
2 │

ℹ matchAll() throws a TypeError when it receives a regular expression without the g (global) flag.

ℹ Add the g flag to the regular expression.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

text.matchAll(new RegExp("pattern", "i"));
code-block.js:1:15 lint/nursery/useGlobalRegexInMatchAll ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The regular expression passed to matchAll() is missing the g flag.

> 1 │ text.matchAll(new RegExp("pattern", "i"));
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ matchAll() throws a TypeError when it receives a regular expression without the g (global) flag.

ℹ Add the g flag to the regular expression.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

const pattern = /pattern/u;
text.matchAll(pattern);
code-block.js:2:15 lint/nursery/useGlobalRegexInMatchAll ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ The regular expression passed to matchAll() is missing the g flag.

1 │ const pattern = /pattern/u;
> 2 │ text.matchAll(pattern);
│ ^^^^^^^
3 │

ℹ The regular expression is created here.

> 1 │ const pattern = /pattern/u;
│ ^^^^^^^^^^
2 │ text.matchAll(pattern);
3 │

ℹ matchAll() throws a TypeError when it receives a regular expression without the g (global) flag.

ℹ Add the g flag to the regular expression.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

text.matchAll(/pattern/g);
text.matchAll(new RegExp("pattern", "gi"));
text.matchAll("pattern");