oxc-project/oxc · error
Invalid regular expression: Duplicated flag
Error message
Invalid regular expression: Duplicated flag
What it means
Diagnostic from oxlint's eslint/no-invalid-regexp rule (crates/oxc_linter/src/rules/eslint/no_invalid_regexp.rs:19). It reports a regular expression whose flags contain the same letter more than once, e.g. /foo/gg or new RegExp('foo', 'gg'). Every JavaScript engine raises 'SyntaxError: Invalid regular expression: Duplicate flag' for this, so the code fails at parse or construction time. The label marks the span of the duplicated flag.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_invalid_regexp.rs:19
use oxc_allocator::Allocator;
use oxc_ast::{AstKind, ast::Argument};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_regular_expression::{ConstructorParser, Options};
use oxc_span::Span;
use rustc_hash::FxHashSet;
use schemars::JsonSchema;
use serde::Deserialize;
use crate::{
AstNode,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
};
// Use the same prefix with `oxc_regular_expression` crate
fn duplicated_flag_diagnostic(span: Span, flag: &str) -> OxcDiagnostic {
OxcDiagnostic::warn("Invalid regular expression: Duplicated flag")
.with_help(format!("Remove the duplicated '{flag}' flag from the regular expression flags"))
.with_label(span.label(format!("flag '{flag}' already specified")))
}
fn unknown_flag_diagnostic(span: Span, flag: &str) -> OxcDiagnostic {
OxcDiagnostic::warn("Invalid regular expression: Unknown flag")
.with_note("Valid flags are: d (indices), g (global), i (ignore case), m (multiline),\n s (dot all), u (unicode), v (unicode sets), y (sticky)")
.with_label(span.label(format!("flag '{flag}' is not a valid regular expression flag")))
}
fn invalid_unicode_flags_diagnostic(span: Span, is_u_specified: bool) -> OxcDiagnostic {
OxcDiagnostic::warn("Invalid regular expression: `u` and `v` flags should be used alone")
.with_help("Specify only one of 'u' or 'v' flags")
.with_label(span.label(if is_u_specified {
"the 'v' flag cannot be used when the 'u' flag is specified"
} else {
"the 'u' flag cannot be used when the 'v' flag is specified"
}))View on GitHub (pinned to e1e7af627c)
Solutions
- Edit the flags so each letter appears exactly once, e.g. /foo/gi or new RegExp('foo', 'gi')
- If flags are assembled at runtime, deduplicate before constructing: new RegExp(pattern, [...new Set(flags)].join(''))
- Add a pre-lint unit check or editor lint-on-save so the duplicate is caught before CI
Example fix
// before
const re = /abc/gg;
const re2 = new RegExp('abc', 'ii');
// after
const re = /abc/g;
const re2 = new RegExp('abc', 'i'); Defensive patterns
Strategy: validation
Validate before calling
function safeFlags(flags) {
return [...new Set(flags)].join(''); // dedupe before constructing
}
const re = new RegExp(pattern, safeFlags('gg')); // 'g' Type guard
function hasDuplicateFlags(flags) {
return typeof flags === 'string' && new Set(flags).size !== flags.length;
} Try / catch
try {
const re = new RegExp(pattern, flags);
} catch (e) {
if (e instanceof SyntaxError && /duplicate flag/i.test(e.message)) {
// dedupe flags and retry, or surface a config error
}
} Prevention
- Lint on save so duplicated flags are flagged at edit time
- Never build flag strings by repeated concatenation; keep flags in one constant
- Prefer regex literals over new RegExp when the pattern and flags are static
When it happens
Trigger: A regex literal with a repeated flag letter (/x/ii, /abc/gg); new RegExp(pattern, flags) or RegExp(pattern, flags) where the flags string repeats a letter, e.g. new RegExp('x', 'dd'). Both regex literals and RegExp constructor calls with literal string arguments are inspected.
Common situations: Hand-editing flags onto an existing regex and typing a letter that is already there (adding 'u' to /x/u); flags built dynamically with string concatenation that appends a flag twice after a merge; copy-paste of a regex from a diff where a flag edit was applied on both sides.
Related errors
- Invalid regular expression: Unknown flag
- Invalid regular expression: `u` and `v` flags should be used
- A regular expression literal can be confused with '/='.
- Empty character class will not match anything
- Unexpected surrogate pair in character class.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/3ac81ed4ef16565c.
Report an issue: GitHub.