Skip to content

useGlobalRegexInReplaceAll (JavaScript)

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

Require regular expressions passed to replaceAll() to use the global flag.

When String.prototype.replaceAll() receives a regular expression, that regular expression must have the g (global) flag. Without it, replaceAll() throws a TypeError when the code runs, instead of replacing any text.

This rule checks regular expression literals, such as /\s+/, and calls to RegExp() or new RegExp() whose flags are written directly in the code. It also follows variables that are initialized with one of these and never reassigned.

withSpaces.replaceAll(/\s+/, ",");
code-block.js:1:23 lint/nursery/useGlobalRegexInReplaceAll ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This regular expression passed to replaceAll() is missing the g flag.

> 1 │ withSpaces.replaceAll(/\s+/, ",");
│ ^^^^^
2 │

ℹ When replaceAll() receives a regular expression without the g (global) flag, it throws a TypeError instead of replacing text.

ℹ Add the g flag to the regular expression, or use replace() to replace only the first match.

ℹ 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.

withSpaces.replaceAll(new RegExp("\\s+", "i"), ",");
code-block.js:1:23 lint/nursery/useGlobalRegexInReplaceAll ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This regular expression passed to replaceAll() is missing the g flag.

> 1 │ withSpaces.replaceAll(new RegExp("\\s+", "i"), ",");
│ ^^^^^^^^^^^^^^^^^^^^^^^
2 │

ℹ When replaceAll() receives a regular expression without the g (global) flag, it throws a TypeError instead of replacing text.

ℹ Add the g flag to the regular expression, or use replace() to replace only the first match.

ℹ 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 spaces = /\s+/;
withSpaces.replaceAll(spaces, ",");
code-block.js:2:23 lint/nursery/useGlobalRegexInReplaceAll ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

✖ This regular expression passed to replaceAll() is missing the g flag.

1 │ const spaces = /\s+/;
> 2 │ withSpaces.replaceAll(spaces, ",");
│ ^^^^^^
3 │

ℹ The regular expression is created here.

> 1 │ const spaces = /\s+/;
│ ^^^^^
2 │ withSpaces.replaceAll(spaces, ",");
3 │

ℹ When replaceAll() receives a regular expression without the g (global) flag, it throws a TypeError instead of replacing text.

ℹ Add the g flag to the regular expression, or use replace() to replace only the first match.

ℹ 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.

withSpaces.replaceAll(/\s+/g, ",");
withSpaces.replaceAll(new RegExp("\\s+", "g"), ",");
withSpaces.replaceAll(" ", ",");
withSpaces.replace(/\s+/, ",");