disallow empty character classes in regular expressions (no-empty-character-class)
禁止在正则表达式中出现空字符集 (no-empty-character-class)
The "extends": "eslint:recommended"
property in a configuration file enables this rule.
配置文件中的 "extends": "eslint:recommended"
属性启用了此规则。
Because empty character classes in regular expressions do not match anything, they might be typing mistakes.
在正则表达式中空字符集不能匹配任何字符,它们可能是打字错误。
var 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
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"]
Known Limitations
This rule does not report empty character classes in the string argument of calls to the RegExp
constructor.
该规则不会报告 RegExp
构造函数的字符串参数中空字符集的使用情况。
Example of a false negative when this rule reports correct code:
当该规则报告了正确的代码时,漏报的示例:
/*eslint no-empty-character-class: "error"*/
var abcNeverMatches = new RegExp("^abc[]");
Version
This rule was introduced in ESLint 0.22.0.
该规则在 ESLint 0.22.0 中被引入。