Skip to content

noControlCharactersInRegex

诊断类别:lint/suspicious/noControlCharactersInRegex

¥Diagnostic Category: lint/suspicious/noControlCharactersInRegex

自从:v1.0.0

¥Since: v1.0.0

来源:

¥Sources:

防止在正则表达式中使用控制字符和一些与控制字符匹配的转义序列。

¥Prevents from having control characters and some escape sequences that match control characters in regular expressions.

控制字符是隐藏的特殊字符,在 ASCII 系统中编号为 0 到 31。它们在 JavaScript 文本中不常用。所以,如果你在模式(称为正则表达式)中看到它们,那可能是一个错误。

¥Control characters are hidden special characters that are numbered from 0 to 31 in the ASCII system. They’re not commonly used in JavaScript text. So, if you see them in a pattern (called a regular expression), it’s probably a mistake.

正则表达式模式的以下元素被视为可能出现的输入错误,因此本规则不允许:

¥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

  • \u0000\u001F 的 Unicode 字符转义

    ¥Unicode character escapes from \u0000 to \u001F

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

    ¥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

¥Invalid

var pattern1 = /\x00/;
code-block.js:1:18 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected control character in a regular expression.

> 1 │ var pattern1 = /\x00/;
^^^^
2 │

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

var pattern2 = /\x0C/;
code-block.js:1:18 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected control character in a regular expression.

> 1 │ var pattern2 = /\x0C/;
^^^^
2 │

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

var pattern3 = /\x1F/;
code-block.js:1:18 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected control character in a regular expression.

> 1 │ var pattern3 = /\x1F/;
^^^^
2 │

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

var pattern4 = /\u000C/;
code-block.js:1:18 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected control character in a regular expression.

> 1 │ var pattern4 = /\u000C/;
^^^^^^
2 │

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

var pattern5 = /\u{C}/u;
code-block.js:1:18 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected control character in a regular expression.

> 1 │ var pattern5 = /\u{C}/u;
^^^^^
2 │

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

var pattern7 = new RegExp("\x0C");
code-block.js:1:29 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected control character in a regular expression.

> 1 │ var pattern7 = new RegExp(“\x0C”);
^^^^
2 │

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

var pattern7 = new RegExp("\\x0C");
code-block.js:1:29 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Unexpected control character in a regular expression.

> 1 │ var pattern7 = new RegExp(“\\x0C”);
^^^^^
2 │

Control characters are unusual and potentially incorrect inputs, so they are disallowed.

¥Valid

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

¥Related links