Skip to content

regexEmptyStringLiterals

Reports empty string literals in character classes.

✅ This rule is included in the ts logical and logicalStrict 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.

A \q{} with no content matches the empty string.

const
const pattern: RegExp
pattern
= /[\q{}]/v;

Even with other elements in the character class, an empty \q{} is reported.

const
const pattern: RegExp
pattern
= /[a\q{}]/v;

The rule also checks regex patterns in RegExp constructor calls.

const
const pattern: RegExp
pattern
= new
var RegExp: RegExpConstructor
new (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp
("[\\q{}]", "v");

If the string literal has at least one non-empty alternative, it is valid.

const
const pattern: RegExp
pattern
= /[\q{a|}]/v;

This rule is not configurable.

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.

Made with ❤️‍🔥 around the world by the Flint team and contributors.