Skip to content

regexUnusedCapturingGroups

Reports capturing groups in regular expressions that are never referenced.

✅ This rule is included in the ts logical presets.

Reports capturing groups in regular expressions that have no backreferences. A capturing group without a backreference is considered “unused” and can typically be converted to a non-capturing group for clarity.

const
const pattern: RegExp
pattern
= /(a)/;
const
const pattern: RegExp
pattern
= /(?<name>a)/;
const
const pattern: RegExp
pattern
= /(a)(b)\1/; // (b) is never referenced
const
const pattern: RegExp
pattern
= new
var RegExp: RegExpConstructor
new (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp
("(a)");

This rule is not configurable.

If you use capturing groups for purposes other than backreferences within the regex itself (such as extracting matched groups via match() or exec()), you might prefer to disable this rule. However, note that modern JavaScript provides named groups which are more readable for extraction purposes. You might consider using Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.

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