regexEmptyStringLiterals
Reports empty string literals in character classes.
✅ This rule is included in the tslogicalandlogicalStrictpresets.
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 const pattern: RegExp
pattern = /[\q{}]/v;Error ts(1501) ― const const pattern: RegExp
pattern = /[\q{a}]/v;Error ts(1501) ― 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 const pattern: RegExp
pattern = /[a\q{}]/v;Error ts(1501) ― const const pattern: RegExp
pattern = /[a]?/v;Error ts(1501) ― RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const const pattern: RegExp
pattern = new var RegExp: RegExpConstructornew (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp("[\\q{}]", "v");const const pattern: RegExp
pattern = new var RegExp: RegExpConstructornew (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
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 const pattern: RegExp
pattern = /[\q{a|}]/v;Error ts(1501) ― 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 ❤️🔥 around the world by
the Flint team and contributors.