oxc-project/oxc · warning
A regular expression literal can be confused with '/='.
Error message
A regular expression literal can be confused with '/='.
What it means
This diagnostic comes from the `no_div_regex` rule in oxlint. It reports a regular expression literal whose pattern starts with the character `=`. Such a literal, for example `/=/`, reads like the division-assignment operator `/=`. The rule walks the parsed regex AST and checks the first `Term` of the pattern for an `=` character.
Source
Thrown at crates/oxc_linter/src/rules/eslint/no_div_regex.rs:10
use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_regular_expression::ast::{CharacterKind, Term};
use oxc_span::Span;
use crate::{AstNode, context::LintContext, rule::Rule};
fn no_div_regex_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("A regular expression literal can be confused with '/='.")
.with_help("Rewrite `/=` into `/[=]`")
.with_label(span)
}
#[derive(Debug, Default, Clone)]
pub struct NoDivRegex;
declare_oxc_lint!(
/// ### What it does
///
/// Disallow equal signs explicitly at the beginning of regular expressions.
///
/// ### Why is this bad?
///
/// Characters /= at the beginning of a regular expression literal can be confused with a
/// division assignment operator.
///
/// ### ExamplesView on GitHub (pinned to e1e7af627c)
Solutions
- Rewrite the literal with a character class: `/[=]/`.
- Use the RegExp constructor when a class is not wanted: `new RegExp('=')`.
- Suppress with `// oxlint-disable-next-line no-div-regex` when the context makes the literal clear.
Example fix
// before
if (/=/.test(token)) { /* assignment operator */ }
// after
if (/[=]/.test(token)) { /* assignment operator */ } Defensive patterns
Strategy: validation
Validate before calling
// crude scan for regex literals starting with '='
if (/(^|[^\\])\/=(?!\/)/.test(src)) console.warn('possible /=/ regex literal; rewrite as /[=]/'); Prevention
- Start regex literals with a character class when the first character is punctuation.
- Keep oxlint running in the editor so the report appears while you type the literal.
When it happens
Trigger: A regex literal whose first character is an equal sign: `var re = /=/;` or `if (/=/.test(token)) { ... }`. The rule inspects the first term of the pattern and its character kind.
Common situations: A developer writes a quick test for the assignment operator character, for example in a tokenizer, a parser, or a lint tool. Humans and simple text tooling read `x /= y` where a regex was meant.
Related errors
- Empty character class will not match anything
- `debugger` statement is not allowed
- Variables should not be deleted
- Duplicate class member: {member_name:?}
- Duplicate conditions in if-else-if chain
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/3f7c37a124a06ec3.
Report an issue: GitHub.