oxc-project/oxc · error · OxcDiagnostic
Global flag (g) is missing in the regular expression supplie
Error message
Global flag (g) is missing in the regular expression supplied to the `matchAll` method.
What it means
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.
Source
Thrown at crates/oxc_linter/src/rules/oxc/bad_match_all_arg.rs:14
use oxc_ast::{AstKind, ast::RegExpFlags};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{
AstNode,
ast_util::{is_method_call, resolve_regex_flags},
context::LintContext,
rule::Rule,
};
fn bad_match_all_arg_diagnostic(match_all_span: Span, regex_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
"Global flag (g) is missing in the regular expression supplied to the `matchAll` method.",
)
.with_help("Add the global flag (g) to the regular expression.")
.with_note("`matchAll` throws a `TypeError` when passed a non-global regular expression.")
.with_labels([
match_all_span.label("`matchAll` called here"),
regex_span.label("RegExp supplied here"),
])
}
#[derive(Debug, Default, Clone)]
pub struct BadMatchAllArg;
declare_oxc_lint!(
/// ### What it does
///
/// This rule warns when the `matchAll` method is called with a regular expression that does
/// not have the global flag (g).View on GitHub (pinned to e1e7af627c)
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
Example fix
// before
for (const m of text.matchAll(/word/)) { count++; }
// after
for (const m of text.matchAll(/word/g)) { count++; } Defensive patterns
Strategy: type-guard
Validate before calling
function toGlobalRegExp(re) {
return re.global ? re : new RegExp(re.source, re.flags + 'g');
}
// validate before calling
for (const m of text.matchAll(toGlobalRegExp(re))) { /* ... */ } Type guard
const isGlobalRegExp = (re) => re.flags.includes('g');
// TypeScript: const isGlobalRegExp = (re: RegExp): re is RegExp & { global: true } => re.flags.includes('g'); Try / catch
try {
for (const m of text.matchAll(re)) { /* ... */ }
} catch (e) {
if (e instanceof TypeError && e.message.includes('non-global')) {
re = new RegExp(re.source, re.flags + 'g');
} else {
throw e;
}
} Prevention
- 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
When it happens
Trigger: `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`.
Common situations: 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.
Related errors
- Global flag (g) is missing in the regular expression supplie
- Unexpected object literal comparison.
- All `if` blocks contain the same code at the start
- All `if` blocks contain the same code at the end
- Left-hand side of `&&` operator has no effect.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/27e0089910c7b9d0.
Report an issue: GitHub.