{"record":{"id":"4b2731d17ca77fa4","repo":"oxc-project/oxc","slug":"invalid-regular-expression-unknown-flag","errorCode":null,"errorMessage":"Invalid regular expression: Unknown flag","messagePattern":"Invalid regular expression: Unknown flag","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/eslint/no_invalid_regexp.rs","lineNumber":25,"sourceCode":"use rustc_hash::FxHashSet;\nuse schemars::JsonSchema;\nuse serde::Deserialize;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n};\n\n// Use the same prefix with `oxc_regular_expression` crate\nfn duplicated_flag_diagnostic(span: Span, flag: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Invalid regular expression: Duplicated flag\")\n        .with_help(format!(\"Remove the duplicated '{flag}' flag from the regular expression flags\"))\n        .with_label(span.label(format!(\"flag '{flag}' already specified\")))\n}\n\nfn unknown_flag_diagnostic(span: Span, flag: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Invalid regular expression: Unknown flag\")\n        .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)\")\n        .with_label(span.label(format!(\"flag '{flag}' is not a valid regular expression flag\")))\n}\n\nfn invalid_unicode_flags_diagnostic(span: Span, is_u_specified: bool) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Invalid regular expression: `u` and `v` flags should be used alone\")\n        .with_help(\"Specify only one of 'u' or 'v' flags\")\n        .with_label(span.label(if is_u_specified {\n            \"the 'v' flag cannot be used when the 'u' flag is specified\"\n        } else {\n            \"the 'u' flag cannot be used when the 'v' flag is specified\"\n        }))\n}\n\n#[derive(Debug, Default, Clone, Deserialize)]\npub struct NoInvalidRegexp(Box<NoInvalidRegexpConfig>);\n\ndeclare_oxc_lint!(","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_invalid_regexp.rs#L7-L43","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nconst re = /line.pattern/a;\n\n// after\nconst re = /line.pattern/s; // 's' (dotAll) matches newlines with .","handlingStrategy":"validation","validationCode":"const VALID = /^[dgimsuvy]*$/;\nfunction assertValidFlags(flags) {\n  if (!VALID.test(flags)) throw new RangeError(`Invalid regex flags: ${flags}`);\n}\nassertValidFlags(userFlags);\nconst re = new RegExp(pattern, userFlags);","typeGuard":"function isValidRegexFlags(flags) {\n  return typeof flags === 'string' && /^[dgimsuvy]*$/.test(flags); // also rejects duplicates only if you add a Set size check\n}","tryCatchPattern":"try {\n  new RegExp(pattern, flags);\n} catch (e) {\n  if (e instanceof SyntaxError && /invalid regular expression flags/i.test(e.message)) {\n    // fall back to a flagless regex or reject the input\n  }\n}","preventionTips":["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"],"tags":["regex","lint","javascript","syntax-error","flags"],"backgroundTag":"invalid-regex-flags","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"}