8000 docs: clarify no-control-regex rule (#15808) · eslint/eslint@06b1edb · GitHub
[go: up one dir, main page]

Skip to content

Commit 06b1edb

Browse files
authored
docs: clarify no-control-regex rule (#15808)
* docs: clarify no-control-regex rule Fixes #15653 * fix escaped
1 parent 6494e3e commit 06b1edb

File tree

1 file changed

+36
-5
lines changed

1 file changed

+36
-5
lines changed

docs/src/rules/no-control-regex.md

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,31 @@ rule_type: problem
99

1010
Disallows control characters in regular expressions.
1111

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.
1313

1414
## Rule Details
1515

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.
1725

1826
Examples of **incorrect** code for this rule:
1927

2028
```js
2129
/*eslint no-control-regex: "error"*/
2230

23-
var pattern1 = /\x1f/;
24-
var pattern2 = new RegExp("\x1f");
31+
var pattern1 = /\x00/;
32+
var pattern2 = /\x0C/;
33+
var pattern3 = /\x1F/;
34+
var pattern4 = /\u000C/;
35+
var pattern5 = new RegExp("\x0C"); // raw U+000C character in the pattern
36+
var pattern6 = new RegExp("\\x0C"); // \x0C pattern
2537
```
2638

2739
Examples of **correct** code for this rule:
@@ -30,9 +42,28 @@ Examples of **correct** code for this rule:
3042
/*eslint no-control-regex: "error"*/
3143

3244
var pattern1 = /\x20/;
33-
var pattern2 = new RegExp("\x20");
45+
var pattern2 = /\u0020/;
46+
var pattern3 = /\t/;
47+
var pattern4 = /\n/;
48+
var pattern5 = new RegExp("\x20");
49+
var pattern6 = new RegExp("\\t");
50+
var pattern7 = new RegExp("\\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+
new RegExp("\t"); // disallowed since the pattern is: <TAB>
61+
62+
new RegExp("\\t"); // allowed since the pattern is: \t
3463
```
3564

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+
3667
## When Not To Use It
3768

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

0 commit comments

Comments
 (0)
0