oxc-project/oxc · warning · OxcDiagnostic
Use regular expression literal with flags instead of the `Re
Error message
Use regular expression literal with flags instead of the `RegExp` constructor.
What it means
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.
Source
Thrown at crates/oxc_linter/src/rules/eslint/prefer_regex_literals.rs:32
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.
///
/// Examples of **incorrect** code for `{ "disallowRedundantWrapping": true }`:
/// ```js
/// new RegExp(/abc/);
/// new RegExp(/abc/, 'u');
/// ```
///
/// Examples of **correct** code for `{ "disallowRedundantWrapping": true }`:View on GitHub (pinned to e1e7af627c)
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.
Example fix
// before const re = new RegExp(/abc/, "i"); // after const re = /abc/i;
Defensive patterns
Strategy: validation
Validate before calling
# find literal + flags wrapped in RegExp rg -n 'new\s+RegExp\(\s*/.*/\s*,\s*["'"'"']' src/
Prevention
- 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.
When it happens
Trigger: Rule configured with `{ "disallowRedundantWrapping": true }` and code containing `new RegExp(/abc/, "gi")` — a regex-literal first argument plus a string flags second argument.
Common situations: 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.
Related errors
- Regular expression literal is unnecessarily wrapped within a
- 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/d6e4ac01b0473665.
Report an issue: GitHub.