{"record":{"id":"12b98d69816a5633","repo":"oxc-project/oxc","slug":"use-a-regular-expression-literal-instead-of-the-r","errorCode":null,"errorMessage":"Use a regular expression literal instead of the `RegExp` constructor.","messagePattern":"Use a regular expression literal 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":20,"sourceCode":"use serde::{Deserialize, Serialize};\n\nuse oxc_ast::{\n    AstKind,\n    ast::{Argument, Expression},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{\n    AstNode,\n    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)]","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/prefer_regex_literals.rs#L2-L38","documentation":"This is the oxlint `eslint/prefer-regex-literals` rule firing on a `new RegExp(...)` (or bare `RegExp(...)`) call whose arguments are statically known, meaning a regex literal would work identically. The diagnostic constructor at prefer_regex_literals.rs:20 is emitted whenever the rule sees a `RegExp` callee with string (or regex-literal) arguments. It is a port of ESLint's prefer-regex-literals and exists because literal regexes are parsed once at compile time, are easier to read, and avoid accidental runtime recompilation.","triggerScenarios":"Calling `RegExp(\"foo\")`, `new RegExp(\"foo\", \"i\")`, or `RegExp('a' + 'b')` where all arguments are string literals or static concatenations of literals. The rule's `is_regexp_callee`/`is_string_raw_member_expression` utils (imported at the top of prefer_regex_literals.rs) gate the check to callee name `RegExp` with string-only arguments.","commonSituations":"Code copied from older ES5 codebases that predate universal regex-literal support; dynamically built patterns where the dynamic part was later removed; generated code. Teams enabling the `eslint` plugin preset in `.oxlintrc.json` without remembering this rule is on.","solutions":["Replace the constructor call with a regex literal: `new RegExp(\"abc\", \"i\")` becomes `/abc/i`.","If the pattern is built from truly static pieces, inline the final pattern as a literal.","If the pattern genuinely must stay dynamic, keep `new RegExp` and disable the rule for the line with `// oxlint-disable-next-line prefer-regex-literals`.","Turn the rule off in `.oxlintrc.json` via `\"rules\": { \"prefer-regex-literals\": \"off\" }` if your team disagrees with it."],"exampleFix":"// before\nconst re = new RegExp(\"^\\\\d+$\", \"i\");\n\n// after\nconst re = /^\\d+$/i;","handlingStrategy":"validation","validationCode":"# fail early in CI on RegExp constructor calls with static args\nrg -n --pcre2 'new\\s+RegExp\\(\\s*[\"'\"'\"']' src/ && echo 'prefer-regex-literals violations' && exit 1","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to writing regex literals; reach for `new RegExp` only when the pattern is built at runtime.","Enable `prefer-regex-literals` (and optionally `disallowRedundantWrapping`) in `.oxlintrc.json` so violations surface locally, not just in CI.","Add `oxlint --fix` to a pre-commit hook; this rule family has safe autofixes."],"tags":["eslint","oxlint","regex","style","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-14T05:17:10.506Z"}