Skip to content
← Back to rules

typescript/prefer-regexp-exec Style

💭 This rule requires type information.

它的作用

在提取正则表达式匹配结果时,优先使用 RegExp#exec() 而不是 String#match()

为什么这不好?

exec() 对正则表达式匹配的表达方式更明确,并且避免了 String#match() 的重载行为。

示例

以下是此规则的错误代码示例:

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

以下是此规则的正确代码示例:

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

如何使用

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

版本

此规则添加于 v1.49.0。

参考资料