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
无用的反向引用是代码中可能出现的错误。它通常表示正则表达式没有按预期工作。
规则详情
sbMWRgtt1YkSpX3ZN2Otm+1cn3TB6ICYFchjikMtW/ZXu5hOJrVjOMTyK6X1BPwsT5H1J7KE2xtKYreviJh3oXaANT/BLpa+P3w48yXqH6E=
- Ky+vJQ6K3PHxg/31WYW7iU5lPbhbU/H0xqMUxRh3J3NxiS+tGeJre65EPfEDf9u27Nk3/ddeJB8wBnQtWEZ3SLMytN+LYiYMsgXcuf5zcOoK5Y+IwaxFWjqTXDSkcuXWjFzasKLvCIl7xG7ZEbvT++Y3QibPfRMujLIZfR/C07zBazXCDvAId1eFtCgKnm3/SnO8RjxY5ujib9VYZMwdgsbTzte8a0Ml8vwL17Ef1yStFplPeOA/NUfvstZg+cvRZowcGkcFBP/1lrf4xz1v5vYrjZMOsUagC+ZfuNpBoY2xTaqd8HsHloKlbpIZUNDTg54rbTBlfOj6uyvuI5HqLBSeU8MqS8zMHVvWc6gmdCcAUplUuc9YEuoC/BqoHdCSui45e/H4QviaqbX8X1wlcIOLd+AHuaJRqOajt5dj0h/6U5TfeCQ9lA7YxD9Xbhy1v0HDQavets9mYJGUZ8lKYXTU2Ma5mkVDl9IjaLCFVc4jydRgINzXVD1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f1exLDgtQf1iomjh98wQ5ygsuoLLWWSr28nb4HRMCgpO7EmTPSMwTYazWzOwTa1Xng0U8tG3fABA/R24rwrjvwqscrbycDxY2a+D7vjt
fxmKIwN3Wqub81VwEwfNk6cS/PW/fyHh0k3Xv9SsFeijwqhE0+dt6IqUcXP3HwtvQJXLt/om58Leu2Q9n4fpfVKD8y4RgxelmYdrhqP5EJoKMFnErm6sVA5oD5QTAvcsIRTtF1/DXMzNW6i0MK6ugqQdcxmsVXGoUnRvGDFcgw4k3FtBcNzunXp210+GHO7S
// in some other languages, this pattern would successfully match "aab"
/^(?:(a)(?=a)|\1b)+$/.test("aab"); // false
mmxLFhqxXZjHj18gill8d7xoj4WPKV1qaR8noXngSr3Gyf1pD8gWIrWYZavPBCih
/*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
+2jabC1cuAvyDUwnGxKcmwB1ejOLGUWMWD7qViNHzWwgHar8cUzqr2bYbCOTvm1P
/*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
rNGwDcAVaNSBazo8OEyzYcmrsMnILGpii+l/GgmILK9p9+hMUiTbFH2KD0Ju91r8aN238Xzvxc4f8K2aLhLuUxrn1AUimMSwZMFApcqaG4+He7cK4MEBoxgqh+kHZ97Vckfo5im8ctFJxFu8kSuPQU1dcQAGwvq2mC723fdC9SBjUGY6XJ0wsgHv9CazebEkAXwysZSKJYBagSkMkGHS6H/WQmFJAfhNiadbWEyqHhWtTYGQ6dwpeZ0ja9JPunzKaOUO5hLDwehA76lgZT1O9eOwm/TnqStuxHKAHrF+bhTFZqIZJ6tRB3D9LPMMg/Mr/8TJt5CscNYQUGt9cTJVoCG4O8DU+xKYtpHq/48Owum3GtzHZW88JNRDTEhONJoCQ42LPY4BkDVo9LJ5ZF/xsZRYTXEpIrkj9/kzLty0sU3/LBobqYtO8+Sy887NHP2BG4cMxehsoHtLPIoIz4lrTD5eRlzf1bIrzIPCmr9BS83crJg9+W95sHVhlG7rfW4R
+QA0UFWrsZqU1uWS5UoKNOzm36SAvLYILXiQRlFWtJYW3C7EfzcIi/TdFuA2qzBk
/*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.