{"record":{"id":"d6e4ac01b0473665","repo":"oxc-project/oxc","slug":"use-regular-expression-literal-with-flags-instead","errorCode":null,"errorMessage":"Use regular expression literal with flags instead of the `RegExp` constructor.","messagePattern":"Use regular expression literal with flags instead of the `RegExp` constructor\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/prefer_regex_literals.rs","lineNumber":32,"sourceCode":"    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n    utils::{is_regexp_callee, is_string_raw_member_expression},\n};\n\nfn unexpected_regexp_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Use a regular expression literal instead of the `RegExp` constructor.\")\n        .with_label(span)\n}\n\nfn unexpected_redundant_regexp_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Regular expression literal is unnecessarily wrapped within a `RegExp` constructor.\",\n    )\n    .with_label(span)\n}\n\nfn unexpected_redundant_regexp_with_flags_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Use regular expression literal with flags instead of the `RegExp` constructor.\",\n    )\n    .with_label(span)\n}\n\n#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\nstruct PreferRegexLiteralsConfig {\n    /// By default, this rule doesn’t check when a regex literal is unnecessarily wrapped in a `RegExp` constructor call.\n    /// When the option `disallowRedundantWrapping` is set to `true`, the rule will also disallow such unnecessary patterns.\n    ///\n    /// Examples of **incorrect** code for `{ \"disallowRedundantWrapping\": true }`:\n    /// ```js\n    /// new RegExp(/abc/);\n    /// new RegExp(/abc/, 'u');\n    /// ```\n    ///\n    /// Examples of **correct** code for `{ \"disallowRedundantWrapping\": true }`:","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/prefer_regex_literals.rs#L14-L50","documentation":"oxlint `eslint/prefer-regex-literals` with `disallowRedundantWrapping: true`: a regex literal passed to `RegExp` together with a flags argument (`new RegExp(/abc/, \"i\")`) can be written as one literal with inline flags. Emitted by `unexpected_redundant_regexp_with_flags_diagnostic` (prefer_regex_literals.rs:32). Like error 321 it only fires when the `disallowRedundantWrapping` option is turned on.","triggerScenarios":"Rule configured with `{ \"disallowRedundantWrapping\": true }` and code containing `new RegExp(/abc/, \"gi\")` — a regex-literal first argument plus a string flags second argument.","commonSituations":"Pattern copied from an older snippet where the flags were appended via the constructor; refactoring that converted a string pattern to a literal but kept the wrapper for the flags.","solutions":["Merge the flags into the literal: `new RegExp(/abc/, \"i\")` becomes `/abc/i`.","Run `oxlint --fix` to apply the merge automatically.","Disable `disallowRedundantWrapping` in config, or use an inline disable comment, if the form must stay."],"exampleFix":"// before\nconst re = new RegExp(/abc/, \"i\");\n\n// after\nconst re = /abc/i;","handlingStrategy":"validation","validationCode":"# find literal + flags wrapped in RegExp\nrg -n 'new\\s+RegExp\\(\\s*/.*/\\s*,\\s*[\"'\"'\"']' src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer inline flags on literals (`/abc/i`) over passing flags as a constructor argument.","Code-review generated snippets for `RegExp(/…/, '…')` patterns.","Rely on `oxlint --fix` to normalize the form automatically."],"tags":["eslint","oxlint","regex","redundant-code","lint"],"backgroundTag":"prefer-regex-literals","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"}