Back to Biomejs

useNamedCaptureGroup

src/content/docs/linter/rules/use-named-capture-group.mdx

latest9.7 KB
Original Source

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)

How to configure

json
{
	"linter": {
		"rules": {
			"nursery": {
				"useNamedCaptureGroup": "error"
			}
		}
	}
}

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

Invalid

js
/(ba[rz])/;
<pre class="language-text"><code class="language-text">code-block.js:1:2 <a href="https://biomejs.dev/linter/rules/use-named-capture-group">lint/nursery/useNamedCaptureGroup</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Capture group is not named.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>/(ba[rz])/; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Named capture groups improve readability by associating a descriptive name with each match. Use </span><span style="color: lightgreen;"><strong>(?&lt;name&gt;...)</strong></span><span style="color: lightgreen;"> instead of </span><span style="color: lightgreen;"><strong>(...)</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
js
/([0-9]{4})/;
<pre class="language-text"><code class="language-text">code-block.js:1:2 <a href="https://biomejs.dev/linter/rules/use-named-capture-group">lint/nursery/useNamedCaptureGroup</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Capture group is not named.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>/([0-9]&#123;4&#125;)/; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Named capture groups improve readability by associating a descriptive name with each match. Use </span><span style="color: lightgreen;"><strong>(?&lt;name&gt;...)</strong></span><span style="color: lightgreen;"> instead of </span><span style="color: lightgreen;"><strong>(...)</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
js
/(?:ab)(cd)/;
<pre class="language-text"><code class="language-text">code-block.js:1:8 <a href="https://biomejs.dev/linter/rules/use-named-capture-group">lint/nursery/useNamedCaptureGroup</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Capture group is not named.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>/(?:ab)(cd)/; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Named capture groups improve readability by associating a descriptive name with each match. Use </span><span style="color: lightgreen;"><strong>(?&lt;name&gt;...)</strong></span><span style="color: lightgreen;"> instead of </span><span style="color: lightgreen;"><strong>(...)</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
js
new RegExp("(foo)");
<pre class="language-text"><code class="language-text">code-block.js:1:13 <a href="https://biomejs.dev/linter/rules/use-named-capture-group">lint/nursery/useNamedCaptureGroup</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Capture group is not named.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>new RegExp(&quot;(foo)&quot;); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Named capture groups improve readability by associating a descriptive name with each match. Use </span><span style="color: lightgreen;"><strong>(?&lt;name&gt;...)</strong></span><span style="color: lightgreen;"> instead of </span><span style="color: lightgreen;"><strong>(...)</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>
js
RegExp("(foo)");
<pre class="language-text"><code class="language-text">code-block.js:1:9 <a href="https://biomejs.dev/linter/rules/use-named-capture-group">lint/nursery/useNamedCaptureGroup</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Capture group is not named.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>RegExp(&quot;(foo)&quot;); <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Named capture groups improve readability by associating a descriptive name with each match. Use </span><span style="color: lightgreen;"><strong>(?&lt;name&gt;...)</strong></span><span style="color: lightgreen;"> instead of </span><span style="color: lightgreen;"><strong>(...)</strong></span><span style="color: lightgreen;">.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit </span><span style="color: lightgreen;"><a href="https://biomejs.dev/linter/#nursery">https://biomejs.dev/linter/#nursery</a></span><span style="color: lightgreen;"> for more information.</span> </code></pre>

Valid

js
/(?<id>ba[rz])/;
/(?:ba[rz])/;
/ba[rz]/;
/(?<year>[0-9]{4})-(?<month>[0-9]{2})/;
new RegExp("(?<id>foo)");
new RegExp(pattern);
</TabItem> </Tabs>