Skip to content
← Back to rules

typescript/prefer-regexp-exec Nursery

💭 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);

如何使用

要通过配置文件或命令行启用此规则,可以使用:

json
{
  "rules": {
    "typescript/prefer-regexp-exec": "error"
  }
}
bash
oxlint --type-aware --deny typescript/prefer-regexp-exec

参考资料