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 later in the pattern.

What it means

This is an oxlint diagnostic from the ESLint-compatible rule `no-useless-backreference` (variant `Forward`). A regex backreference such as \1 refers to a capture group that is defined later in the pattern, so at evaluation time the group has not yet captured anything and the backreference always matches the empty string. Oxc's regex analyzer flags it because the reference is provably dead code inside the pattern.

Source

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

use oxc_span::Span;

use crate::{AstNode, context::LintContext, rule::Rule, utils::run_on_regex_node};

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

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Move the capture group before the backreference so it is defined earlier, e.g. `/\1(a)/` -> `/(a)\1/`.
  2. If the intent was a literal character, replace the backreference with the literal text or a group that exists before it.
  3. If the reference is intentional documentation, rewrite the pattern (e.g. duplicate the subpattern instead of referencing it).
  4. Disable the rule for the line with an oxlint disable comment (`// oxlint-disable-next-line no-useless-backreference`) only if you are certain the engine you target gives it meaning.

Example fix

// before
const re = /\1(a)/;

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

Strategy: validation

Validate before calling

// Before committing a regex, verify every backreference points to an earlier group
function hasForwardBackreference(source) {
  const groups = [...source.matchAll(/\((\?<?[=!:]?|\?<[^>]+>)?/g)];
  const refs = [...source.matchAll(/\\(\d+)/g)];
  return refs.some(([, n]) => Number(n) > /* approximate check */ 0 && source.indexOf(refs[0][0]) < 0);
}
// Simplest reliable check: group index must already have an opening '(' before the backreference
function check(re) {
  let open = 0;
  for (let i = 0; i < re.source.length; i++) {
    if (re.source[i] === '(') open++;
    const m = /^\\(\d+)/.exec(re.source.slice(i));
    if (m && Number(m[0].slice(1)) > open) return false;
  }
  return true;
}

Prevention

When it happens

Trigger: Writing a regex literal or `new RegExp` where a backreference number points forward, e.g. `/\1(a)/` or `/(?:a)|(?:\2(b))/`-style patterns where the group appears after the reference. Any visitor run over LiteralRegex/RegExp calls in oxc_linter emits this when the group index resolves to a span later than the backreference.

Common situations: Copy-pasted patterns edited so a group moved after its reference; renumbering groups after inserting a new capture group early in the pattern; hand-built dynamic patterns where the author lost track of group order.

Related errors


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