Skip to content
← Back to rules

oxc/bad-replace-all-arg Correctness

This rule is turned on by default.

What it does

This rule warns when the replaceAll method is called with a regular expression that does not have the global flag (g).

Why is this bad?

When called with a regular expression, the replaceAll method requires the global flag (g). Otherwise, it throws a TypeError at runtime instead of performing a replacement.

Examples

Examples of incorrect code for this rule:

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

Examples of correct code for this rule:

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

How to use

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

Version

This rule was added in v0.0.22.

References