oxc-project/oxc · warning · OxcDiagnostic
This pattern can be replaced with `{replacement}`.
Error message
This pattern can be replaced with `{replacement}`. What it means
This is one of the two diagnostics of the oxlint rule `unicorn/prefer-string-replace-all`, produced by its `string_literal` helper. It fires when the regex passed to `String#replace` with the global flag contains no regex metacharacters, so the whole pattern is literally just a string — the message interpolates the equivalent plain-string `{replacement}` and points at the regex span.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs:17
use oxc_ast::{
AstKind,
ast::{Argument, MemberExpression, RegExpFlags},
};
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?
///View on GitHub (pinned to e1e7af627c)
Solutions
- Replace the regex with a string: `str.replaceAll('foo', 'bar')`.
- Run `oxlint --fix` — the rule offers a fixer that rewrites the call via `RuleFixer`.
- If the pattern will grow metacharacters later or you rely on regex semantics, keep `.replace(/.../g, ...)` and disable the rule for that line.
Example fix
// before
str.replace(/foo/g, 'bar');
// after
str.replaceAll('foo', 'bar'); Defensive patterns
Strategy: validation
Validate before calling
// Reserve regex literals for actual patterns
const replaced = str.replaceAll('foo', 'bar');
// CI: npx oxlint --deny-warn unicorn/prefer-string-replace-all src/ Prevention
- Use string arguments for literal replacements; regex only when metacharacters/quantifiers are real.
- Remember `replaceAll` throws on non-global regexes — that failure is a feature, catching intent mistakes.
- When the pattern comes from a variable, always pass it as a string, never `new RegExp(var)` with `replace`.
When it happens
Trigger: `str.replace(/foo/g, 'bar')` — a regex literal with the `g` flag whose parsed terms (via `oxc_regular_expression::ast::Term`) contain only literal characters, no classes/quantifiers/groups/assertions. The rule extracts flags with `extract_regex_flags` and reconstructs the plain string for the message/fix using `oxc_codegen` to unparse the literal terms.
Common situations: Developers habitually typing `/.../g` for replaces, or codemods that regex-escaped plain words; the string form is preferred because `replaceAll('foo', 'bar')` is faster, harder to get wrong, and does not silently become a regex injection point when the pattern is built from variables.
Related errors
- Prefer `String#replaceAll()` over `String#replace()` when us
- Prefer String#startsWith over a regex with a caret.
- Prefer String#endsWith over a regex with a dollar sign.
- Unnecessary escape character {escape_char:?}
- Use uppercase characters for the value of the escape sequenc
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/a5840fd6bafdf475.
Report an issue: GitHub.