regexEmptyCapturingGroups
Reports capturing groups that only capture empty strings.
✅ This rule is included in the tslogicalandlogicalStrictpresets.
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 const pattern: RegExp
pattern = /()/;const const pattern: RegExp
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 const pattern: RegExp
pattern = /(\b)/;const const pattern: RegExp
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 const pattern: RegExp
pattern = /(a*)/;const const pattern: RegExp
pattern = /(a+)/;RegExp Constructor
Section titled “RegExp Constructor”The rule also checks regex patterns in RegExp constructor calls.
const const pattern: RegExp
pattern = new var RegExp: RegExpConstructornew (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
RegExp("()");const const pattern: RegExp
pattern = new var RegExp: RegExpConstructornew (pattern: RegExp | string, flags?: string) => RegExp (+2 overloads)
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 ❤️🔥 around the world by
the Flint team and contributors.