Versions

no-misleading-character-class

Disallow characters which are made with multiple code points in character class syntax

Recommended

The "extends": "eslint:recommended" property in a configuration file enables this rule

💡 hasSuggestions

Some problems reported by this rule are manually fixable by editor suggestions

Unicode 包括由多个代码点组成的字符。 正则字符类语法(/[abc]/)不能处理由多个代码点组成的字符,这些字符将被溶入每个代码点。例如,❇️ 是由 U+2747)和 VARIATION SELECTOR-16(U+FE0F)组成。如果这个字符在正则字符类中,它将与 U+2747)或 VARIATION SELECTOR-16(U+FE0F)匹配,而不是 ❇️

这条规则报告在字符类语法中包括多个码位字符的正则表达式。这条规则认为以下字符是多码点字符。

带有组合字符的字符

组合字符是属于 McMeMn 之一的字符 Unicode 一般类别

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

规则细节

这条规则报告在字符类语法中包含多个码位字符的正则表达式。

使用此规则的错误示例:

/*eslint no-misleading-character-class: error */

/^[Á]$/u
/^[❇️]$/u
/^[👶🏻]$/u
/^[🇯🇵]$/u
/^[👨‍👩‍👦]$/u
/^[👍]$/

使用此规则的正确示例:

/*eslint no-misleading-character-class: error */

/^[abc]$/
/^[👍]$/u

何时不用

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

Version

This rule was introduced in ESLint v5.3.0.

Resources