no-empty-character-class
禁止在正则表达式中使用空字符类
在 配置文件 中使用来自 @eslint/js 的 recommended 配置可以启用此规则
因为正则表达式中的空字符类不匹配任何内容,它们可能是输入错误。
🌐 Because empty character classes in regular expressions do not match anything, they might be typing mistakes.
const foo = /^abc[]/;
规则详情
🌐 Rule Details
此规则不允许在正则表达式中使用空字符类。
🌐 This rule disallows empty character classes in regular expressions.
此规则的错误代码示例:
🌐 Examples of incorrect code for this rule:
/*eslint no-empty-character-class: "error"*/
/^abc[]/.test("abcdefg"); // false
"abcdefg".match(/^abc[]/); // null
/^abc[[]]/v.test("abcdefg"); // false
"abcdefg".match(/^abc[[]]/v); // null
/^abc[[]--[x]]/v.test("abcdefg"); // false
"abcdefg".match(/^abc[[]--[x]]/v); // null
/^abc[[d]&&[]]/v.test("abcdefg"); // false
"abcdefg".match(/^abc[[d]&&[]]/v); // null
const regex = /^abc[d[]]/v;
regex.test("abcdefg"); // true, the nested `[]` has no effect
"abcdefg".match(regex); // ["abcd"]
regex.test("abcefg"); // false, the nested `[]` has no effect
"abcefg".match(regex); // null
regex.test("abc"); // false, the nested `[]` has no effect
"abc".match(regex); // null
符合此规则的正确代码示例:
🌐 Examples of correct code for this rule:
/*eslint no-empty-character-class: "error"*/
/^abc/.test("abcdefg"); // true
"abcdefg".match(/^abc/); // ["abc"]
/^abc[a-z]/.test("abcdefg"); // true
"abcdefg".match(/^abc[a-z]/); // ["abcd"]
/^abc[^]/.test("abcdefg"); // true
"abcdefg".match(/^abc[^]/); // ["abcd"]
选项
🌐 Options
此规则没有选项。
🌐 This rule has no options.
已知限制
🌐 Known Limitations
这个规则不会报告在调用 RegExp 构造函数的字符串参数中出现的空字符类。
🌐 This rule does not report empty character classes in the string argument of calls to the RegExp constructor.
当此规则报告正确代码时,假阴性 的示例:
🌐 Example of a false negative when this rule reports correct code:
/*eslint no-empty-character-class: "error"*/
const abcNeverMatches = new RegExp("^abc[]");
版本
此规则是在 ESLint v0.22.0 中引入。