Back to Biomejs

noUselessEscapeInRegex

src/content/docs/linter/rules/no-useless-escape-in-regex.mdx

latest5.3 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v1.9.0` - Diagnostic Category: [`lint/complexity/noUselessEscapeInRegex`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`no-useless-escape`](https://eslint.org/docs/latest/rules/no-useless-escape)

How to configure

json
{
	"linter": {
		"rules": {
			"complexity": {
				"noUselessEscapeInRegex": "error"
			}
		}
	}
}

Description

Disallow unnecessary escape sequence in regular expression literals.

Escaping non-special characters in regular expression literals doesn't have any effect. Hence, they may confuse a reader.

Examples

Invalid

js
/\a/;
<pre class="language-text"><code class="language-text">code-block.js:1:2 <a href="https://biomejs.dev/linter/rules/no-useless-escape-in-regex">lint/complexity/noUselessEscapeInRegex</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The character doesn't need to be escaped.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>/&#92;a/; <strong> │ </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;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Unescape the character.</span> <strong> 1 │ </strong>/<span style="color: Tomato;">&#92;</span>a/; <strong> │ </strong> <span style="color: Tomato;">-</span> </code></pre>
js
/[\-]/;
<pre class="language-text"><code class="language-text">code-block.js:1:3 <a href="https://biomejs.dev/linter/rules/no-useless-escape-in-regex">lint/complexity/noUselessEscapeInRegex</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The character doesn't need to be escaped.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>/[&#92;-]/; <strong> │ </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;">The character should only be escaped if it appears in the middle of the character class or under the &#96;v&#96; flag.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Unescape the character.</span> <strong> 1 │ </strong>/[<span style="color: Tomato;">&#92;</span>-]/; <strong> │ </strong> <span style="color: Tomato;">-</span> </code></pre>
js
/[\&]/v;
<pre class="language-text"><code class="language-text">code-block.js:1:3 <a href="https://biomejs.dev/linter/rules/no-useless-escape-in-regex">lint/complexity/noUselessEscapeInRegex</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The character doesn't need to be escaped.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>/[&#92;&amp;]/v; <strong> │ </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;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Unescape the character.</span> <strong> 1 │ </strong>/[<span style="color: Tomato;">&#92;</span>&amp;]/v; <strong> │ </strong> <span style="color: Tomato;">-</span> </code></pre>

Valid

js
/\^\d\b/
js
/[\b]/
</TabItem> </Tabs>