# no-useless-backreference

> Disallow regular expression backreferences that always match the empty string

A backreference such as `\1` or `\k<quote>` matches the text that a group captured. If the group can't have captured anything by the time the backreference is reached, the backreference matches the empty string, and the pattern accepts more than it seems to.

That happens when the group comes later in the pattern, when the backreference is inside the group, when the group is in another alternative of a `|`, or when the group is in a negative lookaround.

Patterns passed as a string to `RegExp` are checked too.

This rule is on by default.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

On by default as a warning. To turn it off:

```toml bunfig.toml
[lint.rules]
"no-useless-backreference" = "off" # or "error"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 4} */}
```js
const quoted = /\1.*?(["'])/;
const either = /(a)|\1b/;
const doubled = new RegExp("(\\w\\1)");
const notQuoted = /(?!(["']))\w+\1/;
```

Not reported:

{/* good */}
```js
const quoted = /(["']).*?\1/;
const either = /(a)\1|b/;
const doubled = new RegExp("(\\w)\\1");
```
