# prefer-regexp-exec

Rule `unicorn/prefer-regexp-exec`. Prefer `RegExp#exec()` over `String#match()` for non-global regular expressions.

- Source: unicorn
- Technologies: JavaScript
- Kind: style
- Needs type information: no
- Autofix: yes, in at least one linter
- Reach: 9.9M weekly downloads of presets that enable it
- Trend: +28.3% last quarter
- Rank: #1078 by reach, #1333 by growth

## Linters

| Linter | Rule name | Recommended | Autofix | Details | Docs |
| --- | --- | --- | --- | --- | --- |
| ESLint | `unicorn/prefer-regexp-exec` | no | safe | package `eslint-plugin-unicorn` | [docs](https://github.com/sindresorhus/eslint-plugin-unicorn/blob/v80.0.0/docs/rules/prefer-regexp-exec.md) |

## Code examples

### Incorrect: the rule reports this

```js
const match = text.match(/uni(corn)/);
```

```js
const match = text.match(pattern);
```

### Correct: the rule accepts this

```js
const match = /uni(corn)/.exec(text);
```

```js
const match = pattern.exec(text);
```

Examples from the ESLint documentation.

## Enable it

### ESLint: `eslint.config.js`

```js
import unicorn from "eslint-plugin-unicorn";

export default [
  {
    plugins: { unicorn },
    rules: {
      "unicorn/prefer-regexp-exec": "error",
    },
  },
];
```

## Presets that enable it

| Preset | Linter | Package | Rules | Weekly downloads | Trend |
| --- | --- | --- | --- | --- | --- |
| [Unicorn recommended](https://lintradar.dev/presets/unicorn-recommended.md) | ESLint | `eslint-plugin-unicorn` | 340 | 9.5M | +30.3% |
| [XO](https://lintradar.dev/presets/xo.md) | ESLint | `eslint-config-xo` | 618 | 413K | −5.0% |

## Related rules

- [sonarjs/prefer-regexp-exec](https://lintradar.dev/rules/sonarjs/prefer-regexp-exec.md): "RegExp.exec()" should be preferred over "String.match()"
- [typescript/prefer-regexp-exec](https://lintradar.dev/rules/typescript/prefer-regexp-exec.md): Enforce `RegExp#exec` over `String#match` if no global flag is provided
- [unicorn/prefer-promise-try](https://lintradar.dev/rules/unicorn/prefer-promise-try.md): Prefer `Promise.try()` over promise-wrapping boilerplate.
- [unicorn/prefer-scoped-selector](https://lintradar.dev/rules/unicorn/prefer-scoped-selector.md): Prefer `:scope` when using element query selector methods.
- [unicorn/prefer-private-class-fields](https://lintradar.dev/rules/unicorn/prefer-private-class-fields.md): Prefer private class fields over the underscore-prefix convention.
- [unicorn/prefer-simple-condition-first](https://lintradar.dev/rules/unicorn/prefer-simple-condition-first.md): Prefer simple conditions first in logical expressions.

---

Web page: https://lintradar.dev/rules/unicorn/prefer-regexp-exec. Data from npm and the linters' documentation, collected Dec 4, 2026. Site guide for agents: https://lintradar.dev/llms.txt
