Skip to content
← Back to rules

oxc/bad-replace-all-arg Correctness

This rule is turned on by default.

它的作用

replaceAll 方法传入一个没有全局标志(g)的正则表达式时,此规则会发出警告。

为什么这不好?

replaceAll 方法会将字符串的所有出现位置替换为另一个字符串。如果正则表达式没有使用全局标志(g),则只会替换字符串的第一次出现。

示例

此规则的错误代码示例:

javascript
withSpaces.replaceAll(/\s+/, ",");

此规则的正确代码示例:

javascript
withSpaces.replaceAll(/\s+/g, ",");

如何使用

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

json
{
  "rules": {
    "oxc/bad-replace-all-arg": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  rules: {
    "oxc/bad-replace-all-arg": "error",
  },
});
bash
oxlint --deny oxc/bad-replace-all-arg

版本

此规则于 v0.0.22 中添加。

参考资料