useGlobalRegexInReplaceAll (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled “Summary”- Diagnostic Category:
lint/nursery/useGlobalRegexInReplaceAll - This rule doesn’t have a fix.
- The default severity of this rule is error.
- Sources:
- Same as
oxc/bad-replace-all-arg
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useGlobalRegexInReplaceAll": "error" } } }}Description
Section titled “Description”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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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+/, ",");Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.