Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: add non-capturing groups info to prefer-named-capture-group (fixes #15007) #15009

Merged
merged 1 commit into from Sep 4, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions docs/rules/prefer-named-capture-group.md
@@ -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:

Expand All @@ -30,6 +36,7 @@ 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.
```
Expand Down