Disallow characters which are made with multiple code points in character class syntax
The "extends": "eslint:recommended"
property in a configuration file enables this rule
Some problems reported by this rule are manually fixable by editor suggestions
Unicode includes the characters which are made with multiple code points. 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 βοΈ
.
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.
A character with combining characters:
The combining characters are characters which belong to one of Mc
, Me
, and Mn
Unicode general categories.
/^[AΜ]$/u.test("AΜ") //β false
/^[βοΈ]$/u.test("βοΈ") //β false
A character with Emoji modifiers:
/^[πΆπ»]$/u.test("πΆπ»") //β false
/^[πΆπ½]$/u.test("πΆπ½") //β false
A pair of regional indicator symbols:
/^[π―π΅]$/u.test("π―π΅") //β false
Characters that ZWJ joins:
/^[π¨βπ©βπ¦]$/u.test("π¨βπ©βπ¦") //β false
A surrogate pair without Unicode flag:
/^[π]$/.test("π") //β false
// Surrogate pair is OK if with u flag.
/^[π]$/u.test("π") //β true
This rule reports the regular expressions which include multiple code point characters in character class syntax.
Examples of incorrect code for this rule:
/*eslint no-misleading-character-class: error */
/^[AΜ]$/u
/^[βοΈ]$/u
/^[πΆπ»]$/u
/^[π―π΅]$/u
/^[π¨βπ©βπ¦]$/u
/^[π]$/
Examples of correct code for this rule:
/*eslint no-misleading-character-class: error */
/^[abc]$/
/^[π]$/u
You can turn this rule off if you donβt want to check RegExp character class syntax for multiple code point characters.
This rule was introduced in ESLint v5.3.0.
Β© OpenJS Foundation and other contributors
Licensed under the MIT License.
https://eslint.org/docs/latest/rules/no-misleading-character-class