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 appears before in the same lookbehind.

What it means

Diagnostic from `no-useless-backreference` (variant `Backward`). Inside a lookbehind, matching proceeds right-to-left, so a backreference that textually follows a group is evaluated before that group has captured; conversely a reference to a group that appears before it in the same lookbehind can never see a captured value in the way the author expects. Oxc flags the lookbehind-internal reference as always matching empty.

Source

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

fn no_useless_backreference_diagnostic(
    span: Span,
    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

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Restructure the pattern so the capture happens outside the lookbehind and the reference sits in the forward direction, e.g. `/(?<=(a))\1/`.
  2. Repeat the subpattern literally inside the lookbehind instead of using a backreference.
  3. Use a lookahead after the group: `/(a)(?<=\1)/`-style ordering where the engine evaluates captures first.
  4. Add an inline disable comment if you specifically target an engine with different lookbehind semantics.

Example fix

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

// after
const re = /(?<=(a))\1x/;
Defensive patterns

Strategy: validation

Validate before calling

// Avoid backreferences inside lookbehinds; assert with a quick sanity grep before commit
const usesBackrefInLookbehind = /\(\?<=.*(\\\d+|\\k<[^>]+>).*\)/.test(reSource);

Prevention

When it happens

Trigger: A regex containing a lookbehind where a backreference and its group both live inside the same lookbehind, e.g. `/(?<=(a)\1)/` or `/(?<=\1(a))/`. The rule fires when the referenced group's span sits inside the same lookbehind as the backreference.

Common situations: Porting PCRE-style patterns that rely on right-to-left capture semantics; trying to assert repeated content immediately before a position using a backreference inside `(?<=...)`.

Related errors


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