Versions

no-empty-character-class

Disallow empty character classes in regular expressions

Recommended

The "extends": "eslint:recommended" property in a configuration file enables this rule

因为正则表达式中的空字符类并不匹配任何东西,它们可能是打字错误。

var foo = /^abc[]/;

规则细节

这条规则不允许正则表达式中的空字符类。

使用此规则的错误示例:

/*eslint no-empty-character-class: "error"*/

/^abc[]/.test("abcdefg"); // false
"abcdefg".match(/^abc[]/); // null

使用此规则的正确示例:

/*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"]

已知限制

本规则不报告调用 RegExp 构造函数的字符串参数中的空字符类。

当此规则报告正确的代码时,有一个 false negative 的示例:

/*eslint no-empty-character-class: "error"*/

var abcNeverMatches = new RegExp("^abc[]");

Version

This rule was introduced in ESLint v0.22.0.

Resources