{"record":{"id":"27e0089910c7b9d0","repo":"oxc-project/oxc","slug":"global-flag-g-is-missing-in-the-regular-expressi","errorCode":null,"errorMessage":"Global flag (g) is missing in the regular expression supplied to the `matchAll` method.","messagePattern":"Global flag \\(g\\) is missing in the regular expression supplied to the `matchAll` method\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/oxc/bad_match_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_match_all_arg_diagnostic(match_all_span: Span, regex_span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Global flag (g) is missing in the regular expression supplied to the `matchAll` method.\",\n    )\n    .with_help(\"Add the global flag (g) to the regular expression.\")\n    .with_note(\"`matchAll` throws a `TypeError` when passed a non-global regular expression.\")\n    .with_labels([\n        match_all_span.label(\"`matchAll` called here\"),\n        regex_span.label(\"RegExp supplied here\"),\n    ])\n}\n\n#[derive(Debug, Default, Clone)]\npub struct BadMatchAllArg;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// This rule warns when the `matchAll` method is called with a regular expression that does\n    /// not have the global flag (g).","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/oxc/bad_match_all_arg.rs#L1-L32","documentation":"Oxlint rule `oxc/bad-match-all-arg` reports a `String.prototype.matchAll` call whose RegExp argument is missing the global flag (g). As the diagnostic's note states, `matchAll` throws `TypeError: String.prototype.matchAll called with a non-global RegExp argument` at runtime, so this lint fires before the code ever runs. The rule resolves regex flags (including `new RegExp(pattern, flags)` forms) and labels both the call site and the regex.","triggerScenarios":"`text.matchAll(/word/)` (literal without g); `text.matchAll(new RegExp('ab+c'))` (flags string lacks g); regexes originally written for `.match()`/`.exec()` reused with `matchAll`.","commonSituations":"Migrating `.match()` or `.exec()` loops to `matchAll` and forgetting the flag; constructing RegExp dynamically where the flags string is a variable; code paths where the TypeError only appears in production when the call is first reached.","solutions":["Add the global flag to the RegExp literal: `/word/g`","If the regex is built dynamically, ensure the flags include g: `new RegExp(pattern, flags.includes('g') ? flags : flags + 'g')`","If only the first match is needed, use `.match(regex)` or `.exec()` instead of `matchAll`","Enable `oxc/bad-match-all-arg` (correctness category) in your oxlint CI config so regressions fail the build"],"exampleFix":"// before\nfor (const m of text.matchAll(/word/)) { count++; }\n\n// after\nfor (const m of text.matchAll(/word/g)) { count++; }","handlingStrategy":"type-guard","validationCode":"function toGlobalRegExp(re) {\n  return re.global ? re : new RegExp(re.source, re.flags + 'g');\n}\n// validate before calling\nfor (const m of text.matchAll(toGlobalRegExp(re))) { /* ... */ }","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  for (const m of text.matchAll(re)) { /* ... */ }\n} catch (e) {\n  if (e instanceof TypeError && e.message.includes('non-global')) {\n    re = new RegExp(re.source, re.flags + 'g');\n  } else {\n    throw e;\n  }\n}","preventionTips":["Write the g flag in the same keystroke as matchAll/replaceAll","Add unit tests that execute every matchAll call site so the TypeError surfaces locally","Keep `oxc/bad-match-all-arg` enabled in CI (correctness category) so it fails before merge"],"tags":["oxlint","oxc","regexp","matchall","static-analysis","javascript"],"backgroundTag":"matchall-non-global-regexp","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}