oxc-project/oxc · error
Invalid regular expression: Unknown flag
Error message
Invalid regular expression: Unknown flag
What it means
Diagnostic from oxlint's eslint/no-invalid-regexp rule (crates/oxc_linter/src/rules/eslint/no_invalid_regexp.rs:25). It reports a flag letter that is not a valid ECMAScript regular expression flag; the note lists the valid set d, g, i, m, s, u, v, y. Engines throw 'SyntaxError: Invalid regular expression flags' for such input, so this is a guaranteed runtime/parse failure, not a style issue.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_invalid_regexp.rs:25
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"
}))
}
#[derive(Debug, Default, Clone, Deserialize)]
pub struct NoInvalidRegexp(Box<NoInvalidRegexpConfig>);
declare_oxc_lint!(View on GitHub (pinned to e1e7af627c)
Solutions
- Replace the invalid letter with the intended valid flag: d (indices), g (global), i (ignore case), m (multiline), s (dot all), u (unicode), v (unicode sets), y (sticky)
- If the flag came from another regex flavor, port the pattern feature (e.g. Perl /x free-spacing must be removed manually)
- For dynamic flags, validate against /^[dgimsuvy]*$/ before calling new RegExp
Example fix
// before const re = /line.pattern/a; // after const re = /line.pattern/s; // 's' (dotAll) matches newlines with .
Defensive patterns
Strategy: validation
Validate before calling
const VALID = /^[dgimsuvy]*$/;
function assertValidFlags(flags) {
if (!VALID.test(flags)) throw new RangeError(`Invalid regex flags: ${flags}`);
}
assertValidFlags(userFlags);
const re = new RegExp(pattern, userFlags); Type guard
function isValidRegexFlags(flags) {
return typeof flags === 'string' && /^[dgimsuvy]*$/.test(flags); // also rejects duplicates only if you add a Set size check
} Try / catch
try {
new RegExp(pattern, flags);
} catch (e) {
if (e instanceof SyntaxError && /invalid regular expression flags/i.test(e.message)) {
// fall back to a flagless regex or reject the input
}
} Prevention
- Keep flags lowercase; ES flags are case-sensitive
- When porting regexes from other languages (Perl/Python/Java), re-check every flag against the JS set
- Store flags next to the pattern in one config object so they are reviewed together
When it happens
Trigger: A regex literal or RegExp constructor call containing an out-of-set flag letter: /x/a, /pattern/z, new RegExp('x', 'G') (flags are case-sensitive), or a typo like /foo/uu intended as /foo/u.
Common situations: Typos when adding flags (e.g. 's' for dot-all misspelled as 'a'); assuming flags from other regex flavors (Perl's x, Python's L, Java's d) work in JavaScript; uppercase flag letters copied from documentation of another language.
Related errors
- Invalid regular expression: Duplicated 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/4b2731d17ca77fa4.
Report an issue: GitHub.