regexEmptyStringLiterals
Reports empty string literals in character classes.
✅ This rule is included in the ts logical presets.
Reports empty string literals (\q{}) in character classes when using the v (unicodeSets) flag.
An empty string literal in a character class always matches the empty string, which is usually a mistake.
Examples
Section titled “Examples”Empty String Literal
Section titled “Empty String Literal”A \q{} with no content matches the empty string.
const pattern = /[\q{}]/v;const pattern = /[\q{a}]/v;Empty String Literal with Other Elements
Section titled “Empty String Literal with Other Elements”Even with other elements in the character class, an empty \q{} is reported.
const pattern = /[a\q{}]/v;const pattern = /[a]?/v;RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const pattern = new RegExp("[\\q{}]", "v");const pattern = new RegExp("[\\q{abc}]", "v");Non-Empty Alternatives Are Valid
Section titled “Non-Empty Alternatives Are Valid”If the string literal has at least one non-empty alternative, it is valid.
const pattern = /[\q{a|}]/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 empty string literals in character classes to match empty strings and prefer this style over using quantifiers, you might prefer to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-empty-string-literal
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.