oxc-project/oxc · warning · OxcDiagnostic
Regular expression literal is unnecessarily wrapped within a
Error message
Regular expression literal is unnecessarily wrapped within a `RegExp` constructor.
What it means
oxlint `eslint/prefer-regex-literals` with the option `disallowRedundantWrapping: true`: a regex literal wrapped directly in a `RegExp` constructor (`new RegExp(/abc/)`) is redundant because the literal is already a RegExp object. The message comes from `unexpected_redundant_regexp_diagnostic` (prefer_regex_literals.rs:25). Per the config doc comment in the file, this variant is only checked when `disallowRedundantWrapping` is enabled — by default the rule ignores such wrapping.
Source
Thrown at crates/oxc_linter/src/rules/eslint/prefer_regex_literals.rs:25
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{
AstNode,
context::LintContext,
rule::{DefaultRuleConfig, Rule},
utils::{is_regexp_callee, is_string_raw_member_expression},
};
fn unexpected_regexp_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Use a regular expression literal instead of the `RegExp` constructor.")
.with_label(span)
}
fn unexpected_redundant_regexp_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
"Regular expression literal is unnecessarily wrapped within a `RegExp` constructor.",
)
.with_label(span)
}
fn unexpected_redundant_regexp_with_flags_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(
"Use regular expression literal with flags instead of the `RegExp` constructor.",
)
.with_label(span)
}
#[derive(Debug, Default, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
struct PreferRegexLiteralsConfig {
/// By default, this rule doesn’t check when a regex literal is unnecessarily wrapped in a `RegExp` constructor call.
/// When the option `disallowRedundantWrapping` is set to `true`, the rule will also disallow such unnecessary patterns.
///View on GitHub (pinned to e1e7af627c)
Solutions
- Unwrap the literal: `new RegExp(/abc/)` becomes `/abc/`.
- Run `oxlint --fix` — the rule ships an auto-fixer for this variant.
- If the wrapping is intentional (e.g. defensive cloning of a shared regex), disable for the line or set `disallowRedundantWrapping` back to `false` (the default).
Example fix
// before const re = new RegExp(/abc/); // after const re = /abc/;
Defensive patterns
Strategy: validation
Validate before calling
# find regex literals wrapped in a RegExp constructor rg -n 'new\s+RegExp\(\s*/' src/
Prevention
- When refactoring a `new RegExp("...", flags)` into a literal, remove the constructor wrapper in the same commit.
- Keep `disallowRedundantWrapping` enabled in shared configs so redundant wrapping never lands.
- Run `oxlint --fix` in CI or pre-commit; the redundant-wrapping variants are auto-fixable.
When it happens
Trigger: Enabling `{ "disallowRedundantWrapping": true }` in the rule config and writing `new RegExp(/abc/)` or `RegExp(/abc/)` — a single regex-literal argument with no flags argument.
Common situations: Config copied from an ESLint setup that sets disallowRedundantWrapping; refactored code where flags were removed from the inner literal but the wrapper stayed; generated or migrated code.
Related errors
- Use regular expression literal with flags instead of the `Re
- Backreference '{back_reference}' will be ignored. It referen
- Backreference '{back_reference}' will be ignored. It referen
- Backreference '{back_reference}' will be ignored. It referen
- Unnecessary escape character {escape_char:?}
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/2354492557fd9af8.
Report an issue: GitHub.