oxc-project/oxc · warning · OxcDiagnostic

Backreference '{back_reference}' will be ignored. It referen

Error message

Backreference '{back_reference}' will be ignored. It references group '{group}' which is in a negative lookaround.

What it means

Diagnostic from `no-useless-backreference` (variant `IntoNegativeLookaround`). A backreference points at a capture group located inside a negative lookahead/lookbehind `(?!...)` / `(?<!...)`. A negative lookaround succeeds without matching, so its groups never retain a capture, and the backreference always matches the empty string. Oxc's analyzer detects the reference crossing into the negative lookaround and reports it.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_useless_backreference.rs:32

    problem: &Problem,
    back_reference: &str,
    group: &str,
) -> OxcDiagnostic {
    let diagnostic = match problem {
        Problem::Nested => OxcDiagnostic::warn(format!(
            "Backreference '{back_reference}' will be ignored. It references group '{group}' from within that group."
        )),
        Problem::Disjunctive => OxcDiagnostic::warn(format!(
            "Backreference '{back_reference}' will be ignored. It references group '{group}' which is in another alternative."
        )),
        Problem::Forward => OxcDiagnostic::warn(format!(
            "Backreference '{back_reference}' will be ignored. It references group '{group}' which appears later in the pattern."
        )),
        Problem::Backward => OxcDiagnostic::warn(format!(
            "Backreference '{back_reference}' will be ignored. It references group '{group}' which appears before in the same lookbehind."
        )),
        Problem::IntoNegativeLookaround => OxcDiagnostic::warn(format!(
            "Backreference '{back_reference}' will be ignored. It references group '{group}' which is in a negative lookaround."
        )),
    };
    diagnostic
        .with_help("Consider revising the pattern to remove or relocate the backreference so it points to a group that can be matched at the time of evaluation.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoUselessBackreference;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallows backreferences in regular expressions that will always be ignored
    /// because the capture group they refer to has not matched and cannot match
    /// at the time the backreference is evaluated.
    ///
    /// ### Why is this bad?

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Remove the backreference, since the negative-lookaround group is always empty at use time.
  2. Move the capture group into the main pattern or a positive lookaround if you actually need its value: `/(a)(?!b)\1/`-style restructuring.
  3. Duplicate the subpattern instead of referencing the group inside the negative assertion.
  4. Suppress with an inline oxlint disable comment only when targeting an engine with non-standard behavior.

Example fix

// before
const re = /(?!(a))\1x/;

// after
const re = /(?!ax)x/; // assert absence without the dead reference
Defensive patterns

Strategy: validation

Validate before calling

const refsGroupInNegativeLookaround = /\(\?![^)]*\(([^)]*)\)[\s\S]*\\1/.test(reSource);

Prevention

When it happens

Trigger: Patterns like `/(?!(a))\1/` or `/(?<!(a))\1/` where the referenced group is inside a negative lookaround and the backreference is used elsewhere in the pattern.

Common situations: Attempting to reuse text asserted-absent by a negative lookahead; refactoring a positive lookaround to negative while forgetting the backreference; machine-generated patterns combining assertions.

Related errors


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