{"record":{"id":"5a521eed95c12bd2","repo":"oxc-project/oxc","slug":"global-flag-g-is-missing-in-the-regular-expressi-5a521e","errorCode":null,"errorMessage":"Global flag (g) is missing in the regular expression supplied to the `replaceAll` method.","messagePattern":"Global flag \\(g\\) is missing in the regular expression supplied to the `replaceAll` method\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/oxc/bad_replace_all_arg.rs","lineNumber":14,"sourceCode":"use oxc_ast::{AstKind, ast::RegExpFlags};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{\n    AstNode,\n    ast_util::{is_method_call, resolve_regex_flags},\n    context::LintContext,\n    rule::Rule,\n};\n\nfn bad_replace_all_arg_diagnostic(replace_all_span: Span, regex_span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Global flag (g) is missing in the regular expression supplied to the `replaceAll` method.\")\n        .with_help(\"To replace all occurrences of a string, use the `replaceAll` method with the global flag (g) in the regular expression.\")\n        .with_note(\"Unlike `replace`, `replaceAll` throws a `TypeError` when passed a non-global regular expression instead of replacing only the first match.\")\n        .with_labels([\n            replace_all_span.label(\"`replaceAll` called here\"),\n            regex_span.label(\"RegExp supplied here\"),\n        ])\n}\n\n#[derive(Debug, Default, Clone)]\npub struct BadReplaceAllArg;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// This rule warns when the `replaceAll` method is called with a regular expression that does not have the global flag (g).\n    ///\n    /// ### Why is this bad?\n    ///","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/oxc/bad_replace_all_arg.rs#L1-L32","documentation":"Oxlint rule `oxc/bad-replace-all-arg` reports `String.prototype.replaceAll` called with a RegExp lacking the global flag. The diagnostic note is explicit: unlike `replace`, `replaceAll` throws `TypeError: String.prototype.replaceAll called with a non-global RegExp argument` when the regex is non-global. Labels point at the `replaceAll` call and the regex; flags are resolved for literals and `new RegExp` pairs.","triggerScenarios":"`text.replaceAll(/foo/, 'bar')`; `text.replaceAll(new RegExp('a|b'), '-')`; a module-level regex shared with `.replace()`/`.test()` code reused for `replaceAll`.","commonSituations":"Upgrading a single `replace` call to `replaceAll` without adding g; dynamic flags strings that omit 'g'; replacing only the first occurrence intentionally but choosing the wrong method.","solutions":["Add the global flag: `text.replaceAll(/foo/g, 'bar')`","With dynamic construction: `new RegExp(pattern, flags.includes('g') ? flags : flags + 'g')`","If only the first occurrence should be replaced, keep `.replace()` instead of `replaceAll`","Enable `oxc/bad-replace-all-arg` (correctness) in CI"],"exampleFix":"// before\ntext = text.replaceAll(/-/, '_'); // replaces only first, and throws without /g semantics\n\n// after\ntext = text.replaceAll(/-/g, '_');","handlingStrategy":"type-guard","validationCode":"function toGlobalRegExp(re) {\n  return re.global ? re : new RegExp(re.source, re.flags + 'g');\n}\n// validate before calling\ntext = text.replaceAll(toGlobalRegExp(re), 'replacement');","typeGuard":"const isGlobalRegExp = (re) => re.flags.includes('g');\n// TypeScript: const isGlobalRegExp = (re: RegExp): re is RegExp & { global: true } => re.flags.includes('g');","tryCatchPattern":"try {\n  text = text.replaceAll(re, 'replacement');\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('non-global')) {\n    text = text.replaceAll(new RegExp(re.source, re.flags + 'g'), 'replacement');\n  } else {\n    throw e;\n  }\n}","preventionTips":["Pair every `replaceAll` with a global regex; if you want one replacement, use `replace`","Never share a non-global compiled regex between replace/test and replaceAll code paths","Enable `oxc/bad-replace-all-arg` (correctness) in CI"],"tags":["oxlint","oxc","regexp","replaceall","javascript","static-analysis"],"backgroundTag":"replaceall-non-global-regexp","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"}