Skip to content
← Back to rules

typescript/prefer-regexp-exec Style

💭 This rule requires type information.

What it does

Prefer RegExp#exec() over String#match() when extracting a regex match.

Why is this bad?

exec() is more explicit about matching with a regular expression and avoids the overloaded behavior of String#match().

Examples

Examples of incorrect code for this rule:

ts
const text = "value";
text.match(/v/);

Examples of correct code for this rule:

ts
const text = "value";
/v/.exec(text);

How to use

To enable this rule using the config file or in the CLI, you can use:

json
{
  "options": {
    "typeAware": true
  },
  "rules": {
    "typescript/prefer-regexp-exec": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  options: { typeAware: true },
  rules: {
    "typescript/prefer-regexp-exec": "error",
  },
});
bash
oxlint --type-aware --deny typescript/prefer-regexp-exec

Version

This rule was added in v1.49.0.

References