You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/rules/no-control-regex.md
+36-5Lines changed: 36 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -9,19 +9,31 @@ rule_type: problem
9
9
10
10
Disallows control characters in regular expressions.
11
11
12
-
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 these characters is most likely a mistake.
12
+
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.
13
13
14
14
## Rule Details
15
15
16
-
This rule disallows control characters in regular expressions.
16
+
This rule disallows control characters and some escape sequences that match control characters in regular expressions.
17
+
18
+
The following elements of regular expression patterns are considered possible errors in typing and are therefore disallowed by this rule:
19
+
20
+
* Hexadecimal character escapes from `\x00` to `\x1F`.
21
+
* Unicode character escapes from `\u0000` to `\u001F`.
22
+
* Unescaped raw characters from U+0000 to U+001F.
23
+
24
+
Control escapes such as `\t` and `\n` are allowed by this rule.
17
25
18
26
Examples of **incorrect** code for this rule:
19
27
20
28
```js
21
29
/*eslint no-control-regex: "error"*/
22
30
23
-
var pattern1 =/\x1f/;
24
-
var pattern2 =newRegExp("\x1f");
31
+
var pattern1 =/\x00/;
32
+
var pattern2 =/\x0C/;
33
+
var pattern3 =/\x1F/;
34
+
var pattern4 =/\u000C/;
35
+
var pattern5 =newRegExp("\x0C"); // raw U+000C character in the pattern
36
+
var pattern6 =newRegExp("\\x0C"); // \x0C pattern
25
37
```
26
38
27
39
Examples of **correct** code for this rule:
@@ -30,9 +42,28 @@ Examples of **correct** code for this rule:
30
42
/*eslint no-control-regex: "error"*/
31
43
32
44
var pattern1 =/\x20/;
33
-
var pattern2 =newRegExp("\x20");
45
+
var pattern2 =/\u0020/;
46
+
var pattern3 =/\t/;
47
+
var pattern4 =/\n/;
48
+
var pattern5 =newRegExp("\x20");
49
+
var pattern6 =newRegExp("\\t");
50
+
var pattern7 =newRegExp("\\n");
51
+
```
52
+
53
+
## Known Limitations
54
+
55
+
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).
56
+
57
+
```js
58
+
/*eslint no-control-regex: "error"*/
59
+
60
+
newRegExp("\t"); // disallowed since the pattern is: <TAB>
61
+
62
+
newRegExp("\\t"); // allowed since the pattern is: \t
34
63
```
35
64
65
+
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")`.
66
+
36
67
## When Not To Use It
37
68
38
69
If you need to use control character pattern matching, then you should turn this rule off.
0 commit comments