noControlCharactersInRegex (since v1.0.0)
Diagnostic Category: lint/suspicious/noControlCharactersInRegex
Source: no-control-regex
Prevents from having control characters and some escape sequences that match control characters in regular expressions.
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:
- Hexadecimal character escapes from
\x00
to\x1F
- Unicode character escapes from
\u0000
to\u001F
- Unicode code point escapes from
\u{0}
to\u{1F}
- Unescaped raw characters from U+0000 to U+001F
Control escapes such as \t
and \n
are allowed by this rule.
Examples
Section titled ExamplesInvalid
Section titled Invalid var pattern1 = /\x00/;
suspicious/noControlCharactersInRegex.js:1:17 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━
✖ Unexpected control character(s) in regular expression: \x00
> 1 │ var pattern1 = /\x00/;
│ ^^^^^^
2 │
ℹ Control characters are unusual and potentially incorrect inputs, so they are disallowed.
var pattern2 = /\x0C/;
suspicious/noControlCharactersInRegex.js:1:17 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━
✖ Unexpected control character(s) in regular expression: \x0C
> 1 │ var pattern2 = /\x0C/;
│ ^^^^^^
2 │
ℹ Control characters are unusual and potentially incorrect inputs, so they are disallowed.
var pattern3 = /\x1F/;
suspicious/noControlCharactersInRegex.js:1:17 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━
✖ Unexpected control character(s) in regular expression: \x1F
> 1 │ var pattern3 = /\x1F/;
│ ^^^^^^
2 │
ℹ Control characters are unusual and potentially incorrect inputs, so they are disallowed.
var pattern4 = /\u000C/;
suspicious/noControlCharactersInRegex.js:1:17 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━
✖ Unexpected control character(s) in regular expression: \u000C
> 1 │ var pattern4 = /\u000C/;
│ ^^^^^^^^
2 │
ℹ Control characters are unusual and potentially incorrect inputs, so they are disallowed.
var pattern5 = /\u{C}/u;
suspicious/noControlCharactersInRegex.js:1:17 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━
✖ Unexpected control character(s) in regular expression: \u{C}
> 1 │ var pattern5 = /\u{C}/u;
│ ^^^^^^^^
2 │
ℹ Control characters are unusual and potentially incorrect inputs, so they are disallowed.
var pattern7 = new RegExp("\x0C");
suspicious/noControlCharactersInRegex.js:1:17 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━
✖ Unexpected control character(s) in regular expression: \x0C
> 1 │ var pattern7 = new RegExp("\x0C");
│ ^^^^^^^^^^^^^^^^^^
2 │
ℹ Control characters are unusual and potentially incorrect inputs, so they are disallowed.
var pattern7 = new RegExp("\\x0C");
suspicious/noControlCharactersInRegex.js:1:17 lint/suspicious/noControlCharactersInRegex ━━━━━━━━━━━
✖ Unexpected control character(s) in regular expression: \x0C
> 1 │ var pattern7 = new RegExp("\\x0C");
│ ^^^^^^^^^^^^^^^^^^^
2 │
ℹ Control characters are unusual and potentially incorrect inputs, so they are disallowed.
Valid
Section titled Validvar pattern1 = /\x20/;var pattern2 = /\u0020/;var pattern3 = /\u{20}/u;var pattern4 = /\t/;var pattern5 = /\n/;var pattern6 = new RegExp("\x20");