Disallow capturing groups that can match only one word.
configuration in plugin:clean-regex/recommended
: "warn"
Constant capturing groups can only match one word.
Because they can only match one word, they should be replaced with the constant string they capture for better performance. This is especially the case if the capturing groups only matches the empty word.
E.g. /(foo)/
, a()b
, a(\b)
Examples of valid code for this rule:
/(a)/i
/(a|b)/
/(a*)/
/(a)/ // constant but it doesn't match the empty word
Examples of invalid code for this rule:
/()/ // warn about `()`
/(\b)/ // warn about `(\b)`
If this option is set to true
, the rule will ignore capturing groups that can
match non-empty words. This option is true
by default.
Examples of valid code for this rule with ignoreNonEmpty: false
:
/(a)/i
/(a|b)/
/(a*)/
Examples of invalid code for this rule with ignoreNonEmpty: false
:
/(a)/ // warn about `(a)`
/()/ // warn about `()`
/(\b)/ // warn about `(\b)`