no-misleading-character-class

禁止在字符类语法中由多个代码点组成的字符

配置文件 中的 "extends": "eslint:recommended" 属性启用了该规则

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

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

具有组合字符的字符:

lP3WCGie8KS3Wdj88rvt4fMJykgDBK5tHDrNU4hyyraYycbRalt1EKEpX+GpKEcfVw0gNOeGkEykFgc8FHaaC0M4bUSTwlsoQ5UIjPwFy1SY/E0/jCnJ20xQmc9CNp1/9h4NtPtcNg79NOMxEUlgEZT19WMnuCPzo5HKvkyIGQCMyWVXiS8LEMsI0oRTCKcbNExXjkIFyx2rlQngvR+eafL9P+tCZe0BBcs6I/clISo=

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

mcd4cMbATukQ8SkPmoEQBm0VlE//oamqpIq7SAeh81nVzfcnOBX6fKvdYMizi0Rti0S+Zc9YjPy9a3ArZsWiIQ==

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

pKZF3iClUSG/koUyvfjv1npwXK8NesiQ6PLWVA2Y0PiM9DeA41nrEqTHo78wjOge

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

iZquNbj70znOfYHyhZZ5e1CJjjvSwAUBwmh1UKKlir3QjRlKmK9P1CmAoY7CYq6n

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

98Sk3L0vnrLEas8+cuJ/u1Rplao5+eH1wB5nyqCiOBb+Ne9wAXO1BCnF+4AmjDIJJLAD4JLOzuwQ71bk8R5jCA==

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

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

规则详情

q4SVCybagm4x4UKSnJixm5Wjnvm0jCvOAA0tnK1NUDedmGzeNq7dtKWcDZp2dplWlWLoUtgAPpC0E2hC57OJE39+6DJJnxzp6VKnL0xOLLrdZ+qnT8egDbFgOBhrMc2P

RcKra7ltwcYILSI938QSHqeDUJNF18dCoW/jnWHvHMqaGbJjs/cbfGmCaTTyoOih

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

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

HuZdr89SsSFFHS0WvJtjpxEp0Co9MKRIcy8aDCD9kElD4NuTDQ+687mcbM02zKTG

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

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

何时不使用

epMFqvEeXzPqCYyg3cpOVwSsA6Ecf+I9RCmuY2srGFP2BLLN/oGuV/pgxWKY9khBsHBMGpNj5oSMh8qDmEJYDtiTFdQExFtWdbKQKPuIFSXJl6pJMRbH3C5EbJ4Ak/WAu5anpW3nSOjg/j6ZjJRo3g==