Disable the prefer-named-capture-group
rule
#226
Replies: 2 comments
-
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: const regex = /(?<year>[0-9]{4})/; Alternatively, if your intention is not to capture the results, but only express the alternative, use a non-capturing group: const regex = /(?:cauli|sun)flower/; Please see the documentation of the rule. |
Beta Was this translation helpful? Give feedback.
-
Funny, I have the exact opposite :) Named capture groups provide a way to document your regular expression inline. Similar to the case where clear variable names don't require comments, using capture groups make regular expressions mostly self-documenting. // I can clearly understand that this regexp parses year, month and day out of a date string, using that specific order.
const date = /(?<year>[0-9]{4})-(?<month>[0-9]{2})-(?<day>[0-9]{2})/u; At the same time, using the regex result in your code becomes way clearer. foo.exec('bar').groups.id;
// vs
foo.exec('bar')[1]; And you don't have to change your index if you add/remove a capturing groups in the middle. Not using capture groups feels like writing functions where you always use Additionally, forcing groups that you are not using the result of, to be non-capturing groups, result in better (and faster?) regular expressions, as people often abuse capturing groups just for modifiers, without event needing the result, resulting in unused "index gaps" in the results. |
Beta Was this translation helpful? Give feedback.
-
There is a proposal to disable the
prefer-named-capture-group
rule.Beta Was this translation helpful? Give feedback.
All reactions