noUselessRegexBackrefs
¥Summary
-
规则生效日期:
v2.0.0¥Rule available since:
v2.0.0 -
诊断类别:
lint/suspicious/noUselessRegexBackrefs¥Diagnostic Category:
lint/suspicious/noUselessRegexBackrefs -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则没有修复方案。
¥This rule doesn’t have a fix.
-
此规则的默认严重级别为 warning。
¥The default severity of this rule is warning.
-
来源:
¥Sources:
-
¥Same as
no-useless-backreference -
与
regexp/no-useless-backreference相同¥Same as
regexp/no-useless-backreference
-
¥How to configure
{ "linter": { "rules": { "suspicious": { "noUselessRegexBackrefs": "error" } } }}¥Description
禁止在正则表达式字面量中使用始终匹配空字符串的无用反向引用。
¥Disallow useless backreferences in regular expression literals that always match an empty string.
反向引用引用先前捕获组的子匹配项,并匹配与该组相同的文本。JavaScript 正则表达式支持两种语法:
¥A backreference refers to the submatch of a previous capturing group and matches the same text as that group. JavaScript regular expression support two syntaxes:
-
\N,其中N是一个从 1 开始的整数,表示第 N 个已声明的捕获组。¥
\NwhereNis a 1-based integer that refers to the N-th declared capturing group. -
\k<name>指的是名为name的捕获组。此语法仅适用于支持 Unicode 的正则表达式,即使用u或v标志的正则表达式。¥
\k<name>that refers to the capturing group namedname. This syntax is only available in Unicode-aware regular expressions, i.e. regular expressions using theuorvflag.
反向引用在引用以下内容时始终匹配空字符串:
¥A backreference always matches an empty string when it refers to:
-
属于另一个备用分支的组。在
/(a)|b\1b/中,组(a)及其反向引用\1位于不同的交替分支中。/(a)|b\1/与(a)|b/等效。¥A group that belongs to another alternate branch. In
/(a)|b\1b/, the group(a)and its backreference\1are in distinct alternate branches./(a)|b\1/is equivalent to(a)|b/. -
出现在反向引用之后的组。在
/\1(a)/中,组(a)在其反向引用\1之后声明。/\1(a)/与(a)/等效。¥A group that appears after the backreference. In
/\1(a)/, the group(a)is declared after its backreference\1./\1(a)/is equivalent to(a)/. -
声明反向引用的组。在
/(\1)/中,反向引用嵌套在其指向的组中。/(\1)/与/()/等效。¥A group in which the backreference is declared. In
/(\1)/, the backrefernce is nested in the group it refers to./(\1)/is equivalent to/()/. -
位于不包含反向引用的否定环视断言中的组。在
/a(?!(b)).\1/中,反向引用位于否定断言中,而其指向的反向引用本身却位于断言之外。/a(?!(b)).\1/与/a(?!(b))./等效。¥A group that is inside a negative lookaround assertion without the backreference. In
/a(?!(b)).\1/, the backrefernce is in a negative assertion while its backreference is outside./a(?!(b)).\1/is equivalent to/a(?!(b))./. -
在后向断言中,声明在反向引用之前的组。在
/(?<=(a)\1)b/中,当它们处于后向断言时,反向引用会出现在组之后。/(?<=(a)\1)b/与/(?<=(a))b/等效。¥A group that is declared before the backreference inside a lookbehind assertion. In
/(?<=(a)\1)b/, the backreference appears after the group while they are in a lookbehind assertion./(?<=(a)\1)b/is equivalent to/(?<=(a))b/.
始终匹配空字符串的反向引用总是能成功匹配,因此毫无用处。
¥A backreference that always matches an empty string is always successfully matched and is therefore useless.
¥Examples
¥Invalid
/(a)|b\1/;code-block.js:1:7 lint/suspicious/noUselessRegexBackrefs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This backreference refers to a group placed in another alternate branch, making it always match an empty string.
> 1 │ /(a)|b\1/;
│ ^^
2 │
ℹ The backreference refers to this group.
> 1 │ /(a)|b\1/;
│ ^^^
2 │
ℹ The alternate separator is here.
> 1 │ /(a)|b\1/;
│ ^
2 │
ℹ Remove the backreference or place it in the same alternate branch as the group.
/\1(a)/;code-block.js:1:2 lint/suspicious/noUselessRegexBackrefs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This backreference refers to a group that appears after it, making it always match an empty string.
> 1 │ /\1(a)/;
│ ^^
2 │
ℹ A backreference must refer to a group defined before its occurrence.
ℹ Remove the backreference.
/(\1)/;code-block.js:1:3 lint/suspicious/noUselessRegexBackrefs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This backreference is nested within the group to which it refers, making it always match an empty string.
> 1 │ /(\1)/;
│ ^^
2 │
ℹ The group starts here.
> 1 │ /(\1)/;
│ ^^^^
2 │
ℹ Remove the backreference or place it outside the group to which it refers.
/a(?!(b)).\1/;code-block.js:1:11 lint/suspicious/noUselessRegexBackrefs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This backreference refers to a group within a negated assertion, making it always match an empty string.
> 1 │ /a(?!(b)).\1/;
│ ^^
2 │
ℹ The backreference refers to this group.
> 1 │ /a(?!(b)).\1/;
│ ^^^
2 │
ℹ The negated assertion is here.
> 1 │ /a(?!(b)).\1/;
│ ^^^
2 │
ℹ Remove the backreference or place it in the negated assertion.
/(?<=(a)\1)b/;code-block.js:1:9 lint/suspicious/noUselessRegexBackrefs ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
⚠ This backreference refers to a group that appears before itself in a lookbehind assertion, making it always match an empty string.
> 1 │ /(?<=(a)\1)b/;
│ ^^
2 │
ℹ The backreference refers to this group.
> 1 │ /(?<=(a)\1)b/;
│ ^^^
2 │
ℹ The lookbehind assertion is here.
> 1 │ /(?<=(a)\1)b/;
│ ^^^^
2 │
ℹ Remove the backreference or place it after the group it refers to.
¥Valid
/(a)\1/;/(?<foo>a)\k<foo>/u;/a(?!(b|c)\1)./;¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号