]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/no-misleading-character-class.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-misleading-character-class.md
1 ---
2 title: no-misleading-character-class
3 rule_type: problem
4 ---
5
6
7
8
9
10 Unicode includes the characters which are made with multiple code points.
11 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 RegExp character class, it will match to either `❇` (`U+2747`) or VARIATION SELECTOR-16 (`U+FE0F`) rather than `❇️`.
12
13 This rule reports the regular expressions which include multiple code point characters in character class syntax. This rule considers the following characters as multiple code point characters.
14
15 **A character with combining characters:**
16
17 The combining characters are characters which belong to one of `Mc`, `Me`, and `Mn` [Unicode general categories](http://www.unicode.org/L2/L1999/UnicodeData.html#General%20Category).
18
19 ```js
20 /^[Á]$/u.test("Á") //→ false
21 /^[❇️]$/u.test("❇️") //→ false
22 ```
23
24 **A character with Emoji modifiers:**
25
26 ```js
27 /^[👶🏻]$/u.test("👶🏻") //→ false
28 /^[👶🏽]$/u.test("👶🏽") //→ false
29 ```
30
31 **A pair of regional indicator symbols:**
32
33 ```js
34 /^[🇯🇵]$/u.test("🇯🇵") //→ false
35 ```
36
37 **Characters that ZWJ joins:**
38
39 ```js
40 /^[👨‍👩‍👦]$/u.test("👨‍👩‍👦") //→ false
41 ```
42
43 **A surrogate pair without Unicode flag:**
44
45 ```js
46 /^[👍]$/.test("👍") //→ false
47
48 // Surrogate pair is OK if with u flag.
49 /^[👍]$/u.test("👍") //→ true
50 ```
51
52 ## Rule Details
53
54 This rule reports the regular expressions which include multiple code point characters in character class syntax.
55
56 Examples of **incorrect** code for this rule:
57
58 ::: incorrect
59
60 ```js
61 /*eslint no-misleading-character-class: error */
62
63 /^[Á]$/u
64 /^[❇️]$/u
65 /^[👶🏻]$/u
66 /^[🇯🇵]$/u
67 /^[👨‍👩‍👦]$/u
68 /^[👍]$/
69 ```
70
71 :::
72
73 Examples of **correct** code for this rule:
74
75 ::: correct
76
77 ```js
78 /*eslint no-misleading-character-class: error */
79
80 /^[abc]$/
81 /^[👍]$/u
82 ```
83
84 :::
85
86 ## When Not To Use It
87
88 You can turn this rule off if you don't want to check RegExp character class syntax for multiple code point characters.