{"record":{"id":"3ac81ed4ef16565c","repo":"oxc-project/oxc","slug":"invalid-regular-expression-duplicated-flag","errorCode":null,"errorMessage":"Invalid regular expression: Duplicated flag","messagePattern":"Invalid regular expression: Duplicated flag","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/eslint/no_invalid_regexp.rs","lineNumber":19,"sourceCode":"use oxc_allocator::Allocator;\nuse oxc_ast::{AstKind, ast::Argument};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_regular_expression::{ConstructorParser, Options};\nuse oxc_span::Span;\nuse 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        }))","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_invalid_regexp.rs#L1-L37","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nconst re = /abc/gg;\nconst re2 = new RegExp('abc', 'ii');\n\n// after\nconst re = /abc/g;\nconst re2 = new RegExp('abc', 'i');","handlingStrategy":"validation","validationCode":"function safeFlags(flags) {\n  return [...new Set(flags)].join(''); // dedupe before constructing\n}\nconst re = new RegExp(pattern, safeFlags('gg')); // 'g'","typeGuard":"function hasDuplicateFlags(flags) {\n  return typeof flags === 'string' && new Set(flags).size !== flags.length;\n}","tryCatchPattern":"try {\n  const re = new RegExp(pattern, flags);\n} catch (e) {\n  if (e instanceof SyntaxError && /duplicate flag/i.test(e.message)) {\n    // dedupe flags and retry, or surface a config error\n  }\n}","preventionTips":["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"],"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"}