Skip to content

regexEmptyCapturingGroups

Reports capturing groups that only capture empty strings.

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

Reports capturing groups in regular expressions that can only capture zero-length strings. A capturing group that never captures any text is likely a mistake or unnecessary.

A capturing group with no content.

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

Assertions match positions, not characters, so a capturing group containing only assertions captures nothing.

const
const pattern: RegExp
pattern
= /(\b)/;

A quantifier with min=0 like * or ? applied to all elements means the group can match zero characters.

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

This rule is not configurable.

If you intentionally use empty capturing groups for indexing purposes or other as a stylistic choice, you might prefer to disable this rule.

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