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.
Examples
Section titled “Examples”const pattern = /(a)/;const pattern = /(?<name>a)/;const pattern = /(a)(b)\1/; // (b) is never referencedconst pattern = new RegExp("(a)");const pattern = /(a)\1/;// Or use a non-capturing groupconst pattern2 = /(?:a)/;const pattern = /(?<name>a)\k<name>/;// Or use a non-capturing groupconst pattern2 = /(?:a)/;const pattern = /(a)(?:b)\1/;// Or reference both groupsconst pattern2 = /(a)(b)\1\2/;const pattern = new RegExp("(a)\\1");// Or use a non-capturing groupconst pattern2 = new RegExp("(?:a)");Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-unused-capturing-group