# prefer-regexp-exec

Prefer `RegExp.exec` when regular-expression match details are consumed

`String.match` changes its result shape for global patterns and places matching behavior on the input string.
Instead, you SHOULD call `RegExp.exec` for a non-global pattern when consuming one match record.

Global patterns remain with `String.match` because they collect every match rather than one match record.

- Category: style
- Level: warning
- Fix: automatic
- Scope: module

## Reported

```ds title="main.ds"
function firstMatch(text: string): unknown {
    return text.match(/[a-z]+/i);
}
```

## Accepted

```ds title="main.ds"
function firstMatch(text: string): unknown {
    return /[a-z]+/i.exec(text);
}
```

## Prior art

- [typescript-eslint · prefer-regexp-exec](https://typescript-eslint.io/rules/prefer-regexp-exec)

[language/linter/src/rules/style/prefer_regexp_exec.rs:8](https://github.com/destack-sh/destack/blob/main/language/linter/src/rules/style/prefer_regexp_exec.rs#L8)
