eslint/require-unicode-regexp Pedantic
What it does β
Enforce the use of u or v flag on regular expressions.
Why is this bad? β
RegExp u flag has two effects:
- Make the regular expression handling UTF-16 surrogate pairs correctly.
/^[π]$/.test("π") //β false
/^[π]$/u.test("π") //β true- Make the regular expression throwing syntax errors early as disabling Annex B extensions. Because of historical reason, JavaScript regular expressions are tolerant of syntax errors. For example,
/\w{1, 2/is a syntax error, but JavaScript doesnβt throw the error. It matches strings such as"a{1, 2"instead. Such a recovering logic is defined in Annex B.
The RegExp v flag, introduced in ECMAScript 2024, is a superset of the u flag, and offers two more features:
- Unicode properties of strings
const re = /^\p{RGI_Emoji}$/v;
// Match an emoji that consists of just 1 code point:
re.test("β½"); // '\u26BD'
// β true β
// Match an emoji that consists of multiple code points:
re.test("π¨πΎββοΈ"); // '\u{1F468}\u{1F3FE}\u200D\u2695\uFE0F'
// β true β
- Set notation It allows for set operations between character classes:
const re = /[\p{White_Space}&&\p{ASCII}]/v;
re.test("\n"); // β true
re.test("\u2028"); // β falseExamples β
Examples of incorrect code for this rule:
const a = /aaa/;
const b = /bbb/gi;
const c = new RegExp("ccc");
const d = new RegExp("ddd", "gi");Examples of correct code for this rule:
const a = /aaa/u;
const b = /bbb/giu;
const c = new RegExp("ccc", "u");
const d = new RegExp("ddd", "giu");
const e = /aaa/v;
const f = /bbb/giv;
const g = new RegExp("ccc", "v");
const h = new RegExp("ddd", "gv");
// This rule ignores RegExp calls if the flags could not be evaluated to a static value.
function i(flags) {
return new RegExp("eee", flags);
}Configuration β
requireFlag β
type: "u" | "v"
default: null
The u flag may be preferred in environments that do not support the v flag.
Examples of incorrect code for this rule with the { "requireFlag": "u" } option:
const fooEmpty = /foo/;
const fooEmptyRegexp = new RegExp("foo");
const foo = /foo/v;
const fooRegexp = new RegExp("foo", "v");Examples of correct code for this rule with the { "requireFlag": "u" } option:
const foo = /foo/u;
const fooRegexp = new RegExp("foo", "u");The v flag may be a better choice when it is supported because it has more features than the u flag (e.g., the ability to test Unicode properties of strings). It does have a stricter syntax, however (e.g., the need to escape certain characters within character classes).
Examples of incorrect code for this rule with the { "requireFlag": "v" } option:
const fooEmpty = /foo/;
const fooEmptyRegexp = new RegExp("foo");
const foo = /foo/u;
const fooRegexp = new RegExp("foo", "u");Examples of correct code for this rule with the { "requireFlag": "v" } option:
const foo = /foo/v;
const fooRegexp = new RegExp("foo", "v");How to use β
To enable this rule using the config file or in the CLI, you can use:
{
"rules": {
"require-unicode-regexp": "error"
}
}import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"require-unicode-regexp": "error",
},
});oxlint --deny require-unicode-regexpVersion β
This rule was added in v1.63.0.
