Skip to content
This repository has been archived by the owner on Oct 11, 2021. It is now read-only.

Latest commit

 

History

History
61 lines (43 loc) · 1.53 KB

no-unnecessary-group.md

File metadata and controls

61 lines (43 loc) · 1.53 KB

no-unnecessary-group 🔧

Disallow unnecessary non-capturing groups.

configuration in plugin:clean-regex/recommended: "warn"

Source file
Test file

Description

Non-capturing groups which can be removed without changing the meaning of the pattern are unnecessary. E.g. a(?:bc)d == abcd and a(?:b)*c == ab*c

Capturing groups will not be reported or removed.

Examples

Examples of valid code for this rule:

/(?:a|b)c/
/(?:a{2})+/

// will not be removed because...
/(.)\1(?:2\s)/ // ...it would changed the backreference
/\x4(?:1)/     // ...it would complete the hexadecimal escape
/(?:)/         // `//` is not a valid RegExp literal

Examples of invalid code for this rule:

/(?:)a/
/(?:a)/
/(?:a)+/
/a|(?:b|c)/
/foo(?:[abc]*)bar/

allowTop: true

It's sometimes useful to wrap your whole pattern in a non-capturing group (e.g. if the pattern is used as a building block to construct more complex patterns). With this option you can allow top-level non-capturing groups.

Examples of valid code for this rule with allowTop: true:

/(?:ab)/

Examples of invalid code for this rule with allowTop: true:

/(?:a)b/