oxc-project/oxc · warning · OxcDiagnostic

Prefer `String#replaceAll()` over `String#replace()` when us

Error message

Prefer `String#replaceAll()` over `String#replace()` when using a regex with the global flag.

What it means

This is the primary diagnostic of oxlint's `unicorn/prefer-string-replace-all` (emitted by its `use_replace_all` helper). It fires whenever `String#replace` is called with a regex that has the global (`g`) flag, because `String#replaceAll` expresses that intent directly and, unlike `/.../g` with `.replace`, throws helpfully if the regex is not global when given a string — making the intent explicit and the code shorter.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs:22

};
use oxc_codegen::CodegenOptions;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_regular_expression::ast::Term;
use oxc_span::{GetSpan, Span};
use oxc_str::CompactStr;

use crate::{
    AstNode, ast_util::extract_regex_flags, context::LintContext, fixer::RuleFixer, rule::Rule,
};

fn string_literal(span: Span, replacement: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("This pattern can be replaced with `{replacement}`."))
        .with_label(span)
}

fn use_replace_all(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Prefer `String#replaceAll()` over `String#replace()` when using a regex with the global flag.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct PreferStringReplaceAll;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Prefers [`String#replaceAll()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll) over [`String#replace()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) when using a regex with the global flag.
    ///
    /// ### Why is this bad?
    ///
    /// The [`String#replaceAll()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll) method is both faster and safer as you don't have to use a regex and remember to escape it if the string is not a literal. And when used with a regex, it makes the intent clearer.
    ///
    /// ### Examples
    ///
    /// Examples of **incorrect** code for this rule:

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Change `.replace(/pattern/g, x)` to `.replaceAll(/pattern/g, x)` or, if the pattern is plain text, `.replaceAll('pattern', x)`.
  2. Run `oxlint --fix` to convert occurrences automatically.
  3. If you must support pre-ES2021 runtimes, disable the rule: `"unicorn/prefer-string-replace-all": "off"`.
  4. Double-check the regex only replaces where you expect — `replaceAll` with a non-global regex throws `TypeError`, which is the safety improvement this rule leans on.

Example fix

// before
url.replace(/-/g, '_');

// after
url.replaceAll('-', '_');
Defensive patterns

Strategy: validation

Validate before calling

// Prefer the intent-revealing API
const out = url.replaceAll('-', '_');
// CI: npx oxlint --deny-warn unicorn/prefer-string-replace-all src/

Prevention

When it happens

Trigger: Any `someString.replace(/pattern/flags, ...)` call where the regex literal or `new RegExp` includes the `g` flag (detected through `extract_regex_flags` on the argument and a member call named `replace` on a string receiver).

Common situations: Modernization passes over older code that pre-dates ES2021 `replaceAll`; teams on Node >= 15 or modern browsers where `replaceAll` is available. Watch out: on old targets (IE11, old Node), `replaceAll` is a syntax/runtime error, so the rule is wrong unless your support matrix allows it.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/75557b1974878f7f. Report an issue: GitHub.