# no-useless-backreference

禁止正则表达式中无用的反向引用

配置文件中的 "extends": "eslint:recommended" 属性启用了该规则

在 JavaScript 正则表达式中,定义对属于模式的另一个替代部分的组的反向引用、对出现在反向引用之后的组的反向引用、对包含该反向引用的组的反向引用或对位于负环视内的组。但是,根据规范,在任何这些情况下,反向引用总是最终只匹配零长度(空字符串),而不管反向引用和组出现的上下文。

总是成功匹配零长度并且不能匹配其他任何内容的反向引用是无用的。它们基本上被忽略并且可以在不改变正则表达式的行为的情况下被删除。

var regex = /^(?:(a)|\1b)$/;

regex.test("a"); // true
regex.test("b"); // true!
regex.test("ab"); // false

var equivalentRegex = /^(?:(a)|b)$/;

equivalentRegex.test("a"); // true
equivalentRegex.test("b"); // true
equivalentRegex.test("ab"); // false

无用的反向引用是代码中可能出现的错误。它通常表示正则表达式没有按预期工作。

# 规则详情

此规则旨在检测并禁止正则表达式中的以下反向引用:

  • 反向引用另一个备选方案中的组,例如 /(a)|\1b/。在这种构造的正则表达式中,反向引用应该匹配在那个时候,非参与组中捕获的内容。
  • 反向引用模式中稍后出现的组,例如 /\1(a)/。该组尚未捕获任何内容,并且 ECMAScript 不支持前向引用。在向后匹配的内部lookbehinds中,相反的情况适用,并且此规则不允许反向引用出现在相同lookbehind中的组,例如/(?<=(a)\1)b/
  • 反向引用同一组内的组,例如 /(\1)/。与上一个类似,该组还没有捕获任何内容,并且 ECMAScript 不支持嵌套引用。
  • 如果反向引用不在同一个负向环视中,则反向引用在负向环视中的组,例如 /a(?!(b)).\1/。仅当其模式无法匹配时,否定环视(lookahead 或lookbehind)才会成功,这意味着该组已失败。

根据 ECMAScript 规范,上面列出的所有反向引用都是有效的,总是成功匹配零长度,并且不能匹配其他任何内容。因此,它们不会产生解析或运行时错误,也不会影响其正则表达式的行为。它们在语法上有效但无用。

这可能会让来自其他语言的开发人员感到惊讶,因为其中一些反向引用可以以有意义的方式使用。

// in some other languages, this pattern would successfully match "aab"

/^(?:(a)(?=a)|\1b)+$/.test("aab"); // false

此规则的错误代码示例:

/*eslint no-useless-backreference: "error"*/

/^(?:(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

此规则的正确代码示例:

/*eslint no-useless-backreference: "error"*/

/^(?:(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

请注意,此规则并非旨在检测和禁止在正则表达式中可能错误地使用反向引用语法,例如在字符类中使用或尝试引用不存在的组。根据上下文,不是语法上有效的反向引用的 \1...\9 序列可能会产生语法错误,或者被解析为其他内容(例如,作为传统的八进制转义序列)。

此规则的附加正确代码示例:

/*eslint no-useless-backreference: "error"*/

// 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.
Last Updated: 5/13/2023, 8:55:38 PM