---
url: /docs/guide/usage/linter/rules/eslint/prefer-named-capture-group.md
---

### What it does

Enforces the use of named capture groups in regular expressions.

### Why is this bad?

Unnamed capturing groups (`(...)`) are referenced only by position, which
makes the regex harder to read and maintain. When the pattern changes, index-based
references silently break. Named groups (`(?<name>...)`) make the intent explicit
and allow references by name (e.g. `match.groups.year`), which is more robust.

### Examples

Examples of **incorrect** code for this rule:

```js
const re = /([0-9]{4})-([0-9]{2})/;
const match = re.exec(str);
const year = match[1]; // fragile index
```

Examples of **correct** code for this rule:

```js
const re = /(?<year>[0-9]{4})-(?<month>[0-9]{2})/;
const match = re.exec(str);
const year = match.groups.year; // explicit name

// Non-capturing groups are always fine
const parts = /(?:[0-9]{4})/;
```

## How to use

## Version

This rule was added in v1.68.0.

## References
