Skip to content

regexEmptyAlternatives

Reports empty alternatives in regular expressions that may indicate a mistake.

✅ This rule is included in the ts logical and logicalStrict presets.

Empty alternatives in regular expressions match zero characters. While sometimes intentional, they often indicate a mistake such as a typo or incomplete pattern.

An empty alternative at the end of a group or pattern.

const
const pattern: RegExp
pattern
= /a|/;

An empty alternative at the start of a group or pattern.

const
const pattern: RegExp
pattern
= /|a/;

Empty alternatives in capturing and non-capturing groups.

const
const pattern: RegExp
pattern
= /(a|)/;

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
("a|b|");

This rule is not configurable.

If you intentionally use empty alternatives to make a pattern optional and prefer this style over quantifiers, you might prefer to disable this rule. Some developers find (a|) more readable than a? for complex patterns.

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