Back to Biomejs

useUnicodeRegex

src/content/docs/linter/rules/use-unicode-regex.mdx

latest18.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/useUnicodeRegex`](/reference/diagnostics#diagnostic-category) - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`require-unicode-regexp`](https://eslint.org/docs/latest/rules/require-unicode-regexp)

How to configure

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

Description

Enforce the use of the u or v flag for regular expressions.

The u flag (Unicode mode) and v flag (Unicode Sets mode) enable proper handling of Unicode characters in regular expressions. Without these flags, regex patterns may not correctly match Unicode characters like emoji or characters outside the Basic Multilingual Plane.

The u flag was introduced in ES2015 and enables:

  • Correct handling of surrogate pairs (e.g., emoji)
  • Unicode code point escapes (\u{...})
  • Case-insensitive matching for Unicode characters

The v flag was introduced in ES2024 and provides all u flag features plus:

  • Set notation in character classes
  • String literals in character classes
  • Improved Unicode property escapes

Examples

Invalid

js
/foo/;
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/use-unicode-regex">lint/nursery/useUnicodeRegex</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This regular expression is missing the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> or </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>/foo/; <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Without the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> or </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag, this regular expression may not correctly handle Unicode characters, such as emoji or characters outside the Basic Multilingual Plane.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> flag enables Unicode mode which correctly handles surrogate pairs and Unicode escapes. The </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag (ES2024) enables Unicode Sets mode with additional features like set notation in character classes.</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> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Add the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> flag.</span> <strong> 1 │ </strong>/foo/<span style="color: MediumSeaGreen;">u</span>; <strong> │ </strong> <span style="color: MediumSeaGreen;">+</span> </code></pre>
js
/foo/gi;
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/use-unicode-regex">lint/nursery/useUnicodeRegex</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This regular expression is missing the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> or </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>/foo/gi; <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Without the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> or </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag, this regular expression may not correctly handle Unicode characters, such as emoji or characters outside the Basic Multilingual Plane.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> flag enables Unicode mode which correctly handles surrogate pairs and Unicode escapes. The </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag (ES2024) enables Unicode Sets mode with additional features like set notation in character classes.</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> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Add the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> flag.</span> <strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">/</span><span style="color: Tomato;">f</span><span style="color: Tomato;">o</span><span style="color: Tomato;">o</span><span style="color: Tomato;">/</span><span style="color: Tomato;"><strong>g</strong></span><span style="color: Tomato;"><strong>i</strong></span><span style="color: Tomato;">;</span> <strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">/</span><span style="color: MediumSeaGreen;">f</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">/</span><span style="color: MediumSeaGreen;"><strong>g</strong></span><span style="color: MediumSeaGreen;"><strong>i</strong></span><span style="color: MediumSeaGreen;"><strong>u</strong></span><span style="color: MediumSeaGreen;">;</span> <strong>2</strong> <strong>2</strong><strong> │ </strong> </code></pre>
js
new RegExp("foo");
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/use-unicode-regex">lint/nursery/useUnicodeRegex</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This regular expression is missing the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> or </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag.</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><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;">Without the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> or </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag, this regular expression may not correctly handle Unicode characters, such as emoji or characters outside the Basic Multilingual Plane.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> flag enables Unicode mode which correctly handles surrogate pairs and Unicode escapes. The </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag (ES2024) enables Unicode Sets mode with additional features like set notation in character classes.</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", "gi");
<pre class="language-text"><code class="language-text">code-block.js:1:1 <a href="https://biomejs.dev/linter/rules/use-unicode-regex">lint/nursery/useUnicodeRegex</a> <span style="color: #000; background-color: #ddd;"> FIXABLE </span> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">This regular expression is missing the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> or </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag.</span> <strong><span style="color: Tomato;">&gt;</span></strong> <strong>1 │ </strong>new RegExp(&quot;foo&quot;, &quot;gi&quot;); <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>2 │ </strong> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Without the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> or </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag, this regular expression may not correctly handle Unicode characters, such as emoji or characters outside the Basic Multilingual Plane.</span> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">The </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> flag enables Unicode mode which correctly handles surrogate pairs and Unicode escapes. The </span><span style="color: lightgreen;"><strong>v</strong></span><span style="color: lightgreen;"> flag (ES2024) enables Unicode Sets mode with additional features like set notation in character classes.</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> <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Safe fix</span><span style="color: lightgreen;">: </span><span style="color: lightgreen;">Add the </span><span style="color: lightgreen;"><strong>u</strong></span><span style="color: lightgreen;"> flag.</span> <strong>1</strong> <strong> │ </strong><span style="color: Tomato;">-</span> <span style="color: Tomato;">n</span><span style="color: Tomato;">e</span><span style="color: Tomato;">w</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">R</span><span style="color: Tomato;">e</span><span style="color: Tomato;">g</span><span style="color: Tomato;">E</span><span style="color: Tomato;">x</span><span style="color: Tomato;">p</span><span style="color: Tomato;">(</span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">f</span><span style="color: Tomato;">o</span><span style="color: Tomato;">o</span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">,</span><span style="color: Tomato;"><span style="opacity: 0.8;">·</span></span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;"><strong>g</strong></span><span style="color: Tomato;"><strong>i</strong></span><span style="color: Tomato;">&quot;</span><span style="color: Tomato;">)</span><span style="color: Tomato;">;</span> <strong>1</strong><strong> │ </strong><span style="color: MediumSeaGreen;">+</span> <span style="color: MediumSeaGreen;">n</span><span style="color: MediumSeaGreen;">e</span><span style="color: MediumSeaGreen;">w</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">R</span><span style="color: MediumSeaGreen;">e</span><span style="color: MediumSeaGreen;">g</span><span style="color: MediumSeaGreen;">E</span><span style="color: MediumSeaGreen;">x</span><span style="color: MediumSeaGreen;">p</span><span style="color: MediumSeaGreen;">(</span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">f</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">o</span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">,</span><span style="color: MediumSeaGreen;"><span style="opacity: 0.8;">·</span></span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;"><strong>g</strong></span><span style="color: MediumSeaGreen;"><strong>i</strong></span><span style="color: MediumSeaGreen;"><strong>u</strong></span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">)</span><span style="color: MediumSeaGreen;">;</span> <strong>2</strong> <strong>2</strong><strong> │ </strong> </code></pre>

Valid

js
/foo/u;
/foo/v;
/foo/giu;
new RegExp("foo", "u");
new RegExp("foo", "giv");
new RegExp("foo", flags); // dynamic flags are ignored
</TabItem> </Tabs>