regexUnnecessaryNonCapturingGroups
Reports non-capturing groups that can be removed without changing the meaning of the regex.
✅ This rule is included in the ts stylistic presets.
Reports non-capturing groups (?:...) that serve no purpose and can be removed without changing the meaning of the regular expression.
Note that groups are necessary when:
- They apply a quantifier to multiple elements:
/(?:ab)+/ - They group alternations that are not the only element:
/a(?:b|c)d/ - Removing them would create escape sequences:
/\x4(?:1)/ - They wrap a quantified element for an outer quantifier:
/(?:a{2})+/
Examples
Section titled “Examples”const pattern = /(?:abcd)/;const pattern = /(?:a)+/;const pattern = /(?:a|b)/;const pattern = /abcd/;const pattern = /a+/;const pattern = /a|b/;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If you prefer to keep non-capturing groups for readability or organizational purposes, you might want to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.