Versions

no-misleading-character-class

Disallow characters which are made with multiple code points in character class syntax

โœ… Recommended

The "extends": "eslint:recommended" property in a configuration file enables this rule

๐Ÿ’ก hasSuggestions

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

Rule Details

This rule reports the regular expressions which include multiple code point characters in character class syntax.

Examples of incorrect code for this rule:

Open in Playground
/*eslint no-misleading-character-class: error */

/^[Aฬ]$/u;
/^[โ‡๏ธ]$/u;
/^[๐Ÿ‘ถ๐Ÿป]$/u;
/^[๐Ÿ‡ฏ๐Ÿ‡ต]$/u;
/^[๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘ฆ]$/u;
/^[๐Ÿ‘]$/;

Examples of correct code for this rule:

Open in Playground
/*eslint no-misleading-character-class: error */

/^[abc]$/;
/^[๐Ÿ‘]$/u;
/^[\q{๐Ÿ‘ถ๐Ÿป}]$/v;

When Not To Use It

You can turn this rule off if you donโ€™t want to check RegExp character class syntax for multiple code point characters.

Version

This rule was introduced in ESLint v5.3.0.

Resources

Change Language