---
title: "@typescript-eslint/prefer-regexp-exec"
description: "Require `pattern.exec(text)` instead of `text.match(pattern)` when there is no `g` flag"
# Generated by src/codegen/lint-rule-docs.ts. Do not edit below this line.
plugin: "typescript"
aliases: ["typescript-eslint/prefer-regexp-exec","typescript/prefer-regexp-exec"]
kind: "suggestion"
category: "style"
default: "off"
fixable: "yes"
typeAware: true
deprecated: false
---

The return value of `text.match(pattern)` depends on the flags of `pattern`. With `g` it's a list of all matches without their groups. Without `g` it's the first match with its groups, which is what `pattern.exec(text)` returns. If you use `exec` for one match and keep `match` for the `g` flag, the method name tells you which of the two you get.

A call is only reported if the rule can tell that there's no `g` flag. A regular expression that comes in as a parameter is left alone.

`--fix` rewrites the call. A string argument becomes a regular expression, so `text.match("v1")` becomes `/v1/.exec(text)`.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. This rule needs types, so turning it on makes `bun lint` type check your code first.

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/prefer-regexp-exec" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```ts
function version(text: string) {
  return text.match(/v(\d+)/) ?? text.match("latest");
}
```

Not reported:

{/* good */}
```ts
function version(text: string) {
  return /v(\d+)/.exec(text) ?? /latest/.exec(text);
}

function numbers(text: string) {
  return text.match(/\d+/g);
}

function search(text: string, pattern: RegExp) {
  return text.match(pattern);
}
```
