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.
    ///
    /// ### Examples

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rewrite the literal with a character class: `/[=]/`.
  2. Use the RegExp constructor when a class is not wanted: `new RegExp('=')`.
  3. 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

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


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/3f7c37a124a06ec3. Report an issue: GitHub.