]> git.proxmox.com Git - pve-eslint.git/blobdiff - eslint/docs/rules/prefer-named-capture-group.md
import 8.3.0 source
[pve-eslint.git] / eslint / docs / rules / prefer-named-capture-group.md
index ff8790c836d62ed103fa1267c6e0bf4c0afc7b96..2caf9518eabd65718fe85b0f1aa29f5fa757171d 100644 (file)
@@ -1,14 +1,20 @@
 # Suggest using named capture group in regular expression (prefer-named-capture-group)
 
+
+## Rule Details
+
 With the landing of ECMAScript 2018, named capture groups can be used in regular expressions, which can improve their readability.
+This rule is aimed at using named capture groups instead of numbered capture groups in regular expressions:
 
 ```js
 const regex = /(?<year>[0-9]{4})/;
 ```
 
-## Rule Details
+Alternatively, if your intention is not to _capture_ the results, but only express the alternative, use a non-capturing group:
 
-This rule is aimed at using named capture groups instead of numbered capture groups in regular expressions.
+```js
+const regex = /(?:cauli|sun)flower/;
+```
 
 Examples of **incorrect** code for this rule:
 
@@ -30,13 +36,14 @@ Examples of **correct** code for this rule:
 const foo = /(?<id>ba[rz])/;
 const bar = new RegExp('(?<id>ba[rz])');
 const baz = RegExp('(?<id>ba[rz])');
+const xyz = /xyz(?:zy|abc)/;
 
 foo.exec('bar').groups.id; // Retrieve the group result.
 ```
 
 ## When Not To Use It
 
-If you are targeting ECMAScript 2017 and/or older environments, you can disable this rule, because this ECMAScript feature is only supported in ECMAScript 2018 and/or newer environments.
+If you are targeting ECMAScript 2017 and/or older environments, you should not use this rule, because this ECMAScript feature is only supported in ECMAScript 2018 and/or newer environments.
 
 ## Related Rules