useNamedCaptureGroup (JavaScript)
Language JavaScript (and super languages)
Summary
Section titled βSummaryβ- Rule available since:
v2.4.5 - Diagnostic Category:
lint/style/useNamedCaptureGroup - This rule isnβt recommended, so you need to enable it.
- This rule doesnβt have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
prefer-named-capture-group
- Same as
How to configure
Section titled βHow to configureβ{ "linter": { "rules": { "style": { "useNamedCaptureGroup": "error" } } }}Description
Section titled βDescriptionβEnforce using named capture groups in regular expression.
Numbered capture groups like (...) can be difficult to work with,
as they are matched by their position and not by a descriptive name.
Named capture groups ((?<name>...)) associate a descriptive name
with each match, making the regular expression more readable and
its intent clearer.
Examples
Section titled βExamplesβInvalid
Section titled βInvalidβ/(ba[rz])/;code-block.js:1:2 lint/style/useNamedCaptureGroup ββββββββββββββββββββββββββββββββββββββββββββββββββ
βΉ Capture group is not named.
> 1 β /(ba[rz])/;
β ^
2 β
βΉ Named capture groups improve readability by associating a descriptive name with each match. Use (?<name>...) instead of (...).
/([0-9]{4})/;code-block.js:1:2 lint/style/useNamedCaptureGroup ββββββββββββββββββββββββββββββββββββββββββββββββββ
βΉ Capture group is not named.
> 1 β /([0-9]{4})/;
β ^
2 β
βΉ Named capture groups improve readability by associating a descriptive name with each match. Use (?<name>...) instead of (...).
/(?:ab)(cd)/;code-block.js:1:8 lint/style/useNamedCaptureGroup ββββββββββββββββββββββββββββββββββββββββββββββββββ
βΉ Capture group is not named.
> 1 β /(?:ab)(cd)/;
β ^
2 β
βΉ Named capture groups improve readability by associating a descriptive name with each match. Use (?<name>...) instead of (...).
new RegExp("(foo)");code-block.js:1:13 lint/style/useNamedCaptureGroup βββββββββββββββββββββββββββββββββββββββββββββββββ
βΉ Capture group is not named.
> 1 β new RegExp("(foo)");
β ^
2 β
βΉ Named capture groups improve readability by associating a descriptive name with each match. Use (?<name>...) instead of (...).
RegExp("(foo)");code-block.js:1:9 lint/style/useNamedCaptureGroup ββββββββββββββββββββββββββββββββββββββββββββββββββ
βΉ Capture group is not named.
> 1 β RegExp("(foo)");
β ^
2 β
βΉ Named capture groups improve readability by associating a descriptive name with each match. Use (?<name>...) instead of (...).
/(?<id>ba[rz])/;/(?:ba[rz])/;/ba[rz]/;/(?<year>[0-9]{4})-(?<month>[0-9]{2})/;new RegExp("(?<id>foo)");new RegExp(pattern);Related links
Section titled βRelated linksβCopyright (c) 2023-present Biome Developers and Contributors.