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