{"record":{"id":"75557b1974878f7f","repo":"oxc-project/oxc","slug":"prefer-string-replaceall-over-string-replace","errorCode":null,"errorMessage":"Prefer `String#replaceAll()` over `String#replace()` when using a regex with the global flag.","messagePattern":"Prefer `String#replaceAll\\(\\)` over `String#replace\\(\\)` when using a regex with the global flag\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs","lineNumber":22,"sourceCode":"};\nuse oxc_codegen::CodegenOptions;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_regular_expression::ast::Term;\nuse oxc_span::{GetSpan, Span};\nuse oxc_str::CompactStr;\n\nuse crate::{\n    AstNode, ast_util::extract_regex_flags, context::LintContext, fixer::RuleFixer, rule::Rule,\n};\n\nfn string_literal(span: Span, replacement: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"This pattern can be replaced with `{replacement}`.\"))\n        .with_label(span)\n}\n\nfn use_replace_all(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer `String#replaceAll()` over `String#replace()` when using a regex with the global flag.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferStringReplaceAll;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// 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.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// 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.\n    ///\n    /// ### Examples\n    ///\n    /// Examples of **incorrect** code for this rule:","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs#L4-L40","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Change `.replace(/pattern/g, x)` to `.replaceAll(/pattern/g, x)` or, if the pattern is plain text, `.replaceAll('pattern', x)`.","Run `oxlint --fix` to convert occurrences automatically.","If you must support pre-ES2021 runtimes, disable the rule: `\"unicorn/prefer-string-replace-all\": \"off\"`.","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."],"exampleFix":"// before\nurl.replace(/-/g, '_');\n\n// after\nurl.replaceAll('-', '_');","handlingStrategy":"validation","validationCode":"// Prefer the intent-revealing API\nconst out = url.replaceAll('-', '_');\n// CI: npx oxlint --deny-warn unicorn/prefer-string-replace-all src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Confirm ES2021 support (Node 15+, all evergreen browsers) before adopting replaceAll.","Audit `/g` + `.replace` pairs during review — they are exactly the sites this rule targets.","Disable the rule in legacy-support branches rather than working around it in code."],"tags":["oxlint","unicorn","string","regex","es2021"],"backgroundTag":"regex-to-string-replaceall","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}