Index

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.

带组合字符的字符:

组合字符是属于 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

带有表情符号修饰符的字符:

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

一对区域指示符号:

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

由ZWJ连接的字符:

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

没有 Unicode 标志的代理对:

/^[👍]$/.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

使用 { "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 中引入。

资源