regexEmptyCapturingGroups
Reports capturing groups that only capture empty strings.
✅ This rule is included in the ts logical presets.
Reports capturing groups in regular expressions that can only capture zero-length strings. A capturing group that never captures any text is likely a mistake or unnecessary.
Examples
Section titled “Examples”Empty Capturing Group
Section titled “Empty Capturing Group”A capturing group with no content.
const pattern = /()/;const pattern = /(a)/;Capturing Group with Only Assertions
Section titled “Capturing Group with Only Assertions”Assertions match positions, not characters, so a capturing group containing only assertions captures nothing.
const pattern = /(\b)/;const pattern = /\b/;Zero-Length Quantifiers
Section titled “Zero-Length Quantifiers”A quantifier with min=0 like * or ? applied to all elements means the group can match zero characters.
const pattern = /(a*)/;const pattern = /(a+)/;RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const pattern = new RegExp("()");const pattern = 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 intentionally use empty capturing groups for indexing purposes or other as a stylistic choice, you might prefer to disable this rule.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
regexp/no-empty-capturing-group
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.