no-useless-backreference
eslint
No autofixRecommended
eslint.config.js
export default [
{
rules: {
"no-useless-backreference": "error",
},
},
];Disallow useless backreferences in regular expressions
/^(?:(a)|\1b)$/; // reference to (a) into another alternative
/^(?:(a)|b(?:c|\1))$/; // reference to (a) into another alternative
/^(?:a|b(?:(c)|\1))$/; // reference to (c) into another alternative
/\1(a)/; // forward reference to (a)
RegExp('(a)\\2(b)'); // forward reference to (b)
/(?:a)(b)\2(c)/; // forward reference to (c)
/\k<foo>(?<foo>a)/; // forward reference to (?<foo>a)
/(?<=(a)\1)b/; // backward reference to (a) from within the same lookbehind
/(?<!(a)\1)b/; // backward reference to (a) from within the same lookbehind
new RegExp('(\\1)'); // nested reference to (\1)
/^((a)\1)$/; // nested reference to ((a)\1)
/a(?<foo>(.)b\1)/; // nested reference to (?<foo>(.)b\1)
/a(?!(b)).\1/; // reference to (b) into a negative lookahead
/(?<!(a))b\1/; // reference to (a) into a negative lookbehind/^(?:(a)|(b)\2)$/; // reference to (b)
/(a)\1/; // reference to (a)
RegExp('(a)\\1(b)'); // reference to (a)
/(a)(b)\2(c)/; // reference to (b)
/(?<foo>a)\k<foo>/; // reference to (?<foo>a)
/(?<=\1(a))b/; // reference to (a), correctly before the group as they're in the same lookbehind
/(?<=(a))b\1/; // reference to (a), correctly after the group as the backreference isn't in the lookbehind
new RegExp('(.)\\1'); // reference to (.)
/^(?:(a)\1)$/; // reference to (a)
/^((a)\2)$/; // reference to (a)
/a(?<foo>(.)b\2)/; // reference to (.)
/a(?!(b|c)\1)./; // reference to (b|c), correct as it's from within the same negative lookahead
/(?<!\1(a))b/; // reference to (a), correct as it's from within the same negative lookbehind// comments describe behavior in a browser
/^[\1](a)$/.test("\x01a"); // true. In a character class, \1 is treated as an octal escape sequence.
/^\1$/.test("\x01"); // true. Since the group 1 doesn't exist, \1 is treated as an octal escape sequence.
/^(a)\1\2$/.test("aa\x02"); // true. In this case, \1 is a backreference, \2 is an octal escape sequence.Examples from the ESLint documentation.
no-useless-backreference
eslint
export default [
{
rules: {
"no-useless-backreference": "error",
},
},
];no-useless-backreference
eslint plugin · correctness
{
"rules": {
"no-useless-backreference": "error"
}
}suspicious/noUselessRegexBackrefs
suspicious · since 2.0.0
{
"linter": {
"rules": {
"suspicious": {
"noUselessRegexBackrefs": "error"
}
}
}
}Disallow constant expressions in conditions
Disallow duplicate class members
Disallow literal numbers that lose precision
Enforce return statements in getters
Require super() calls in constructors