Back to Biomejs

useRegexpExec

src/content/docs/linter/rules/use-regexp-exec.mdx

latest5.3 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. ::: :::note This rule belongs to the types domain. This means that its activation will activate the Biome Scanner to scan the files of your project, and enable the type inference engine. Read more about it in the [documentation page](/linter/domains#types) ::: ## Summary - Rule available since: `v2.3.9` - Diagnostic Category: [`lint/nursery/useRegexpExec`](/reference/diagnostics#diagnostic-category) - This rule doesn't have a fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - This rule belongs to the following domains: - [`types`](/linter/domains#types) - Sources: - Same as [`@typescript-eslint/prefer-regexp-exec`](https://typescript-eslint.io/rules/prefer-regexp-exec) - Same as [`regexp/prefer-regexp-exec`](https://ota-meshi.github.io/eslint-plugin-regexp/rules/prefer-regexp-exec.html)

How to configure

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

Description

Enforce RegExp#exec over String#match if no global flag is provided.

String#match is defined to work the same as RegExp#exec when the regular expression does not include the g flag. Keeping to consistently using one of the two can help improve code readability.

RegExp#exec may also be slightly faster than String#match; this is the reason to choose it as the preferred usage.

Examples

Invalid

ts
'something'.match(/thing/);
<pre class="language-text"><code class="language-text"><a href="file:///invalid.ts">/invalid.ts</a>:1:1 <a href="https://biomejs.dev/linter/rules/use-regexp-exec">lint/nursery/useRegexpExec</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Prefer </span><span style="color: lightgreen;"><strong>RegExp#exec()</strong></span><span style="color: lightgreen;"> over </span><span style="color: lightgreen;"><strong>String#match()</strong></span><span style="color: lightgreen;"> when searching within a string.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>'something'.match(/thing/); <strong> │ </strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Use </span><span style="color: lightgreen;"><strong>RegExp#exec()</strong></span><span style="color: lightgreen;"> instead of </span><span style="color: lightgreen;"><strong>String#match()</strong></span><span style="color: lightgreen;"> for consistent and slightly faster regex matching.</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

ts
/thing/.exec('something');
</TabItem> </Tabs>