Index

require-unicode-regexp

强制在正则表达式上使用 uv 标志

💡 hasSuggestions

此规则报告的一些问题可通过编辑器 建议 手动修复

正则表达式 u 标志有两个效果:

🌐 RegExp u flag has two effects:

  1. 使正则表达式正确处理 UTF-16 代理对。

    特别是,字符范围语法得到了正确的行为。

    /^[👍]$/.test("👍") //→ false
    /^[👍]$/u.test("👍") //→ true
    
  2. 通过禁用 附录 B 扩展 使正则表达式尽早抛出语法错误。

    由于历史原因,JavaScript 的正则表达式对语法错误是容忍的。例如,/\w{1, 2/ 是语法错误,但 JavaScript 并不会抛出错误。它会匹配诸如 "a{1, 2" 的字符串。这样的恢复逻辑在附录 B 中定义。

    u 标志会禁用附件 B 定义的恢复逻辑。因此,你可以及早发现错误。这类似于 严格模式

ECMAScript 2024 中引入的正则表达式 v 标志是 u 标志的超集,并提供另外两个功能:

🌐 The RegExp v flag, introduced in ECMAScript 2024, is a superset of the u flag, and offers two more features:

  1. 字符串的 Unicode 属性

    通过 Unicode 属性转义,你可以使用字符串的属性。

    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 ✅
    
  2. 集合符号

    它允许在字符类之间进行集合操作。

    const re = /[\p{White_Space}&&\p{ASCII}]/v;
    re.test('\n'); // → true
    re.test('\u2028'); // → false
    

因此,uv 标志让我们能够更好地使用正则表达式。

🌐 Therefore, the u and v flags let us work better with regular expressions.

规则详情

🌐 Rule Details

这个规则旨在强制在正则表达式上使用 uv 标志。

🌐 This rule aims to enforce the use of u or v flag on regular expressions.

此规则的错误代码示例:

🌐 Examples of incorrect code for this rule:

在线运行
/*eslint require-unicode-regexp: error */

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:

在线运行
/*eslint require-unicode-regexp: error */

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", "giv")

// This rule ignores RegExp calls if the flags could not be evaluated to a static value.
function i(flags) {
    return new RegExp("eee", flags)
}

选项

🌐 Options

此规则有一个对象选项:

🌐 This rule has one object option:

  • "requireFlag": "u"|"v" 需要特定的 Unicode 正则表达式标志

requireFlag: “u”

u 标志可能在不支持 v 标志的环境中更受青睐。

🌐 The u flag may be preferred in environments that do not support the v flag.

使用 { "requireFlag": "u" } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "requireFlag": "u" } option:

在线运行
/*eslint require-unicode-regexp: ["error", { "requireFlag": "u" }] */

const fooEmpty = /foo/;

const fooEmptyRegexp = new RegExp('foo');

const foo = /foo/v;

const fooRegexp = new RegExp('foo', 'v');

使用 { "requireFlag": "u" } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { "requireFlag": "u" } option:

在线运行
/*eslint require-unicode-regexp: ["error", { "requireFlag": "u" }] */

const foo = /foo/u;

const fooRegexp = new RegExp('foo', 'u');

requireFlag: “v”

v 标志在支持的情况下可能是更好的选择,因为它比 u 标志具有更多功能(例如,测试字符串的 Unicode 属性的能力)。然而,它确实有更严格的语法(例如,需要在字符类中对某些字符进行转义)。

🌐 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).

使用 { "requireFlag": "v" } 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the { "requireFlag": "v" } option:

在线运行
/*eslint require-unicode-regexp: ["error", { "requireFlag": "v" }] */

const fooEmpty = /foo/;

const fooEmptyRegexp = new RegExp('foo');

const foo = /foo/u;

const fooRegexp = new RegExp('foo', 'u');

使用 { "requireFlag": "v" } 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the { "requireFlag": "v" } option:

在线运行
/*eslint require-unicode-regexp: ["error", { "requireFlag": "v" }] */

const foo = /foo/v;

const fooRegexp = new RegExp('foo', 'v');

何时不使用

🌐 When Not To Use It

如果你不想在正则表达式中没有 uv 标志时发出警告,那么禁用此规则是安全的。

🌐 If you don’t want to warn on regular expressions without either a u or a v flag, then it’s safe to disable this rule.

关于 i 标志和 \w 的说明

🌐 Note on i flag and \w

在某些情况下,由于 Unicode 大小写折叠,将 u 标志添加到同时使用 i 标志和 \w 字符类的正则表达式中可能会改变其行为。

🌐 In some cases, adding the u flag to a regular expression using both the i flag and the \w character class can change its behavior due to Unicode case folding.

例如:

🌐 For example:

const regexWithoutU = /^\w+$/i;
const regexWithU = /^\w+$/iu;

const str = "\u017f\u212a"; // Example Unicode characters

console.log(regexWithoutU.test(str)); // false
console.log(regexWithU.test(str)); // true

如果你在这种特定情况下更愿意使用非 Unicode 感知的正则表达式,你可以使用 eslint-disable 注释来禁用此规则:

🌐 If you prefer to use a non-Unicode-aware regex in this specific case, you can disable this rule using an eslint-disable comment:

/* eslint-disable require-unicode-regexp */
const regex = /^\w+$/i;
/* eslint-enable require-unicode-regexp */

版本

此规则是在 ESLint v5.3.0 中引入。

进阶读物

资源