require-unicode-regexp

强制在 RegExp 上使用 uv 标志

💡 hasSuggestions

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

RegExp u 标志有两个作用:

¥RegExp u flag has two effects:

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

    ¥Make the regular expression handling UTF-16 surrogate pairs correctly.

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

    ¥Especially, character range syntax gets the correct behavior.

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

    ¥Make the regular expression throwing syntax errors early as disabling Annex B extensions.

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

    ¥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.

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

    ¥The u flag disables the recovering logic Annex B defined. As a result, you can find errors early. This is similar to the strict mode.

ECMAScript 2024 中引入的 RegExp 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 properties of strings

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

    ¥With the Unicode property escape, you can use 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 ✅
    
  2. 设置符号

    ¥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'); // → 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"|"v" requires a particular Unicode regex flag

requireFlag:“u”

在不支持 v 标志的环境中,可能首选 u 标志。

¥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.

版本

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

进阶读物

资源

ESLint 中文网
粤ICP备13048890号