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
- Remove the backreference, since the negative-lookaround group is always empty at use time.
- Move the capture group into the main pattern or a positive lookaround if you actually need its value: `/(a)(?!b)\1/`-style restructuring.
- Duplicate the subpattern instead of referencing the group inside the negative assertion.
- 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
- Never reference groups that live inside (?!...) or (?<!...) - they never capture.
- Move needed captures into the main pattern or a positive lookaround.
- Run oxlint on generated/merged regex constants to catch this automatically.
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
- Backreference '{back_reference}' will be ignored. It referen
- Backreference '{back_reference}' will be ignored. It referen
- Unnecessary escape character {escape_char:?}
- Use a regular expression literal instead of the `RegExp` con
- Regular expression literal is unnecessarily wrapped within a
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/b1819d46e8d9a015.
Report an issue: GitHub.