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