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 pattern = /(a)/;
const pattern = /(?<name>a)/;
const pattern = /(a)(b)\1/; // (b) is never referenced
const pattern = new 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 ❤️‍🔥 in Boston by Josh Goldberg and contributors.