regexGraphemeStringLiterals
Reports string literals inside character classes with the
vflag that contain multiple graphemes.
✅ This rule is included in the ts stylisticStrict presets.
When using the v flag (Unicode sets mode), character classes support string literals using the \q{...} syntax.
These string literals are intended to match a single grapheme or character sequence that behaves as a unit.
Using multi-grapheme strings inside these literals confuses the distinction between string matching and character class behavior.
If you need to match multi-character strings, use regular alternation with a non-capturing group instead of string literals inside character classes.
Examples
Section titled “Examples”const pattern = /[\q{abc}]/v;const pattern = /[\q{a|bc}]/v;const pattern = /[\q{hello|world}]/v;const pattern = /[\q{a|b|c}]/v;const pattern = /[\q{©️|®️}]/v;const pattern = /[\q{🇦🇨|🇦🇩}]/v;const pattern = /[\q{👨👩👧👦}]/v;// Use regular alternation for multi-character stringsconst pattern = /(?:hello|world)/v;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you intentionally use multi-grapheme string literals in character classes and understand the semantic difference between string matching and character class behavior, you might prefer to disable this rule.
Further Reading
Section titled “Further Reading”- MDN: Regular expressions - Unicode character class escape
- MDN: Regular expressions - Character class
Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/grapheme-string-literal