src/content/docs/linter/rules/use-named-capture-group.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> :::caution This rule is part of the [nursery](/linter/#nursery) group. This means that it is experimental and the behavior can change at any time. ::: ## Summary - Rule available since: `v2.4.5` - Diagnostic Category: [`lint/nursery/useNamedCaptureGroup`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`prefer-named-capture-group`](https://eslint.org/docs/latest/rules/prefer-named-capture-group){
"linter": {
"rules": {
"nursery": {
"useNamedCaptureGroup": "error"
}
}
}
}
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.
/(ba[rz])/;
/([0-9]{4})/;
/(?:ab)(cd)/;
new RegExp("(foo)");
RegExp("(foo)");
/(?<id>ba[rz])/;
/(?:ba[rz])/;
/ba[rz]/;
/(?<year>[0-9]{4})-(?<month>[0-9]{2})/;
new RegExp("(?<id>foo)");
new RegExp(pattern);