]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # disallow empty character classes in regular expressions (no-empty-character-class) |
2 | ||
3 | Because empty character classes in regular expressions do not match anything, they might be typing mistakes. | |
4 | ||
5 | ```js | |
6 | var foo = /^abc[]/; | |
7 | ``` | |
8 | ||
9 | ## Rule Details | |
10 | ||
11 | This rule disallows empty character classes in regular expressions. | |
12 | ||
13 | Examples of **incorrect** code for this rule: | |
14 | ||
15 | ```js | |
16 | /*eslint no-empty-character-class: "error"*/ | |
17 | ||
18 | /^abc[]/.test("abcdefg"); // false | |
19 | "abcdefg".match(/^abc[]/); // null | |
20 | ``` | |
21 | ||
22 | Examples of **correct** code for this rule: | |
23 | ||
24 | ```js | |
25 | /*eslint no-empty-character-class: "error"*/ | |
26 | ||
27 | /^abc/.test("abcdefg"); // true | |
28 | "abcdefg".match(/^abc/); // ["abc"] | |
29 | ||
30 | /^abc[a-z]/.test("abcdefg"); // true | |
31 | "abcdefg".match(/^abc[a-z]/); // ["abcd"] | |
32 | ``` | |
33 | ||
34 | ## Known Limitations | |
35 | ||
36 | This rule does not report empty character classes in the string argument of calls to the `RegExp` constructor. | |
37 | ||
38 | Example of a *false negative* when this rule reports correct code: | |
39 | ||
40 | ```js | |
41 | /*eslint no-empty-character-class: "error"*/ | |
42 | ||
43 | var abcNeverMatches = new RegExp("^abc[]"); | |
44 | ``` |