no-control-regex

禁止在正则表达式中使用控制字符

Recommended

配置文件 中使用来自 @eslint/jsrecommended 配置可以启用此规则

控制字符是 ASCII 范围 0-31 中的特殊不可见字符。这些字符很少在 JavaScript 字符串中使用,因此包含显式匹配这些字符的元素的正则表达式很可能是错误的。

¥Control characters are special, invisible characters in the ASCII range 0-31. These characters are rarely used in JavaScript strings so a regular expression containing elements that explicitly match these characters is most likely a mistake.

规则详情

¥Rule Details

此规则不允许控制字符和某些与正则表达式中的控制字符匹配的转义序列。

¥This rule disallows control characters and some escape sequences that match control characters in regular expressions.

正则表达式模式的以下元素被认为是可能的键入错误,因此被此规则禁止:

¥The following elements of regular expression patterns are considered possible errors in typing and are therefore disallowed by this rule:

  • 十六进制字符从 \x00 转义到 \x1F

    ¥Hexadecimal character escapes from \x00 to \x1F.

  • Unicode 字符从 \u0000 转义到 \u001F

    ¥Unicode character escapes from \u0000 to \u001F.

  • Unicode 代码点从 \u{0} 转义到 \u{1F}

    ¥Unicode code point escapes from \u{0} to \u{1F}.

  • 从 U+0000 到 U+001F 的未转义原始字符。

    ¥Unescaped raw characters from U+0000 to U+001F.

此规则允许 \t\n 等控制转义。

¥Control escapes such as \t and \n are allowed by this rule.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-control-regex: "error"*/

var pattern1 = /\x00/;
var pattern2 = /\x0C/;
var pattern3 = /\x1F/;
var pattern4 = /\u000C/;
var pattern5 = /\u{C}/u;
var pattern6 = new RegExp("\x0C"); // raw U+000C character in the pattern
var pattern7 = new RegExp("\\x0C"); // \x0C pattern

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-control-regex: "error"*/

var pattern1 = /\x20/;
var pattern2 = /\u0020/;
var pattern3 = /\u{20}/u;
var pattern4 = /\t/;
var pattern5 = /\n/;
var pattern6 = new RegExp("\x20");
var pattern7 = new RegExp("\\t");
var pattern8 = new RegExp("\\n");

已知限制

¥Known Limitations

在检查 RegExp 构造函数调用时,此规则检查评估的正则表达式模式。因此,尽管此规则打算允许诸如 \t 之类的语法,但它不允许 new RegExp("\t"),因为评估的模式("\t" 的字符串值)包含原始控制字符(TAB 字符)。

¥When checking RegExp constructor calls, this rule examines evaluated regular expression patterns. Therefore, although this rule intends to allow syntax such as \t, it doesn’t allow new RegExp("\t") since the evaluated pattern (string value of "\t") contains a raw control character (the TAB character).

/*eslint no-control-regex: "error"*/

new RegExp("\t"); // disallowed since the pattern is: <TAB>

new RegExp("\\t"); // allowed since the pattern is: \t

new RegExp("\t")new RegExp("\\t") 之间的行为没有区别,两种情况下匹配 TAB 字符的意图都很明确。就本规则而言,它们同样有效,但仅允许 new RegExp("\\t")

¥There is no difference in behavior between new RegExp("\t") and new RegExp("\\t"), and the intention to match the TAB character is clear in both cases. They are equally valid for the purpose of this rule, but it only allows new RegExp("\\t").

何时不使用

¥When Not To Use It

如果你需要使用控制字符模式匹配,那么你应该关闭此规则。

¥If you need to use control character pattern matching, then you should turn this rule off.

版本

此规则是在 ESLint v0.1.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号