{"record":{"id":"a5840fd6bafdf475","repo":"oxc-project/oxc","slug":"this-pattern-can-be-replaced-with-replacement","errorCode":null,"errorMessage":"This pattern can be replaced with `{replacement}`.","messagePattern":"This pattern can be replaced with `(.+?)`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs","lineNumber":17,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{Argument, MemberExpression, RegExpFlags},\n};\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    ///","sourceCodeStart":1,"sourceCodeEnd":35,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_string_replace_all.rs#L1-L35","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","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."],"exampleFix":"// before\nstr.replace(/foo/g, 'bar');\n\n// after\nstr.replaceAll('foo', 'bar');","handlingStrategy":"validation","validationCode":"// Reserve regex literals for actual patterns\nconst replaced = str.replaceAll('foo', 'bar');\n// CI: npx oxlint --deny-warn unicorn/prefer-string-replace-all src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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`."],"tags":["oxlint","unicorn","string","regex","replaceall"],"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"}