no-misleading-character-class

不允许在字符类语法中使用多个代码点组成的字符

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

💡 hasSuggestions

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

Unicode 包括由多个代码点组成的字符。RegExp 字符类语法 (/[abc]/) 无法处理由多个代码点构成的字符作为一个字符;这些字符将被分解到每个代码点。例如,❇️ (U+2747) 和 VARIATION SELECTOR-16 (U+FE0F) 制成。如果此字符属于 RegExp 字符类,它将匹配 (U+2747) 或 VARIATION SELECTOR-16 (U+FE0F),而不是 ❇️

¥Unicode includes characters which are made by multiple code points. RegExp character class syntax (/[abc]/) cannot handle characters which are made by multiple code points as a character; those characters will be dissolved to each code point. For example, ❇️ is made by (U+2747) and VARIATION SELECTOR-16 (U+FE0F). If this character is in a RegExp character class, it will match either (U+2747) or VARIATION SELECTOR-16 (U+FE0F) rather than ❇️.

此规则报告在字符类语法中包含多个代码点字符的正则表达式。此规则将以下字符视为多个代码点字符。

¥This rule reports regular expressions which include multiple code point characters in character class syntax. This rule considers the following characters as multiple code point characters.

具有组合字符的字符:

¥A character with combining characters:

组合字符是属于 McMeMn Unicode 常规类别 之一的字符。

¥The combining characters are characters which belong to one of Mc, Me, and Mn Unicode general categories.

/^[Á]$/u.test("Á"); //→ false
/^[❇️]$/u.test("❇️"); //→ false

带有表情符号修饰符的角色:

¥A character with Emoji modifiers:

/^[👶🏻]$/u.test("👶🏻"); //→ false
/^[👶🏽]$/u.test("👶🏽"); //→ false

一对区域指标符号:

¥A pair of regional indicator symbols:

/^[🇯🇵]$/u.test("🇯🇵"); //→ false

ZWJ 加入的字符:

¥Characters that ZWJ joins:

/^[👨‍👩‍👦]$/u.test("👨‍👩‍👦"); //→ false

没有 Unicode 标志的代理对:

¥A surrogate pair without Unicode flag:

/^[👍]$/.test("👍"); //→ false

// Surrogate pair is OK if with u flag.
/^[👍]$/u.test("👍"); //→ true

规则详情

¥Rule Details

此规则报告在字符类语法中包含多个代码点字符的正则表达式。

¥This rule reports regular expressions which include multiple code point characters in character class syntax.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-misleading-character-class: error */

/^[Á]$/u;
/^[❇️]$/u;
/^[👶🏻]$/u;
/^[🇯🇵]$/u;
/^[👨‍👩‍👦]$/u;
/^[👍]$/;
new RegExp("[🎵]");

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-misleading-character-class: error */

/^[abc]$/;
/^[👍]$/u;
/^[\q{👶🏻}]$/v;
new RegExp("^[]$");
new RegExp(`[Á-${z}]`, "u"); // variable pattern

选项

¥Options

此规则有一个对象选项:

¥This rule has an object option:

  • "allowEscape":当设置为 true 时,该规则允许字符类内的任何代码点分组,只要它们是使用转义序列编写的。此选项仅对正则表达式字面量和使用 RegExp 构造函数以字面量参数作为模式创建的正则表达式有效。

    ¥"allowEscape": When set to true, the rule allows any grouping of code points inside a character class as long as they are written using escape sequences. This option only has effect on regular expression literals and on regular expressions created with the RegExp constructor with a literal argument as a pattern.

allowEscape

使用 { "allowEscape": true } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the { "allowEscape": true } option:

在线运行
/* eslint no-misleading-character-class: ["error", { "allowEscape": true }] */

/[\👍]/; // backslash can be omitted

new RegExp("[\ud83d" + "\udc4d]");

const pattern = "[\ud83d\udc4d]";
new RegExp(pattern);

使用 { "allowEscape": true } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the { "allowEscape": true } option:

在线运行
/* eslint no-misleading-character-class: ["error", { "allowEscape": true }] */

/[\ud83d\udc4d]/;
/[\u00B7\u0300-\u036F]/u;
/[👨\u200d👩]/u;
new RegExp("[\x41\u0301]");
new RegExp(`[\u{1F1EF}\u{1F1F5}]`, "u");
new RegExp("[\\u{1F1EF}\\u{1F1F5}]", "u");

何时不使用

¥When Not To Use It

如果你不想检查多个代码点字符的 RegExp 字符类语法,则可以关闭此规则。

¥You can turn this rule off if you don’t want to check RegExp character class syntax for multiple code point characters.

版本

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

资源

ESLint 中文网
粤ICP备13048890号