Skip to content

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})+/
const pattern = /(?:abcd)/;
const pattern = /(?:a)+/;
const pattern = /(?:a|b)/;

This rule is not configurable.

If you prefer to keep non-capturing groups for readability or organizational purposes, you might want to disable this rule.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.