Eslint
规则 | Rules

no-empty-character-class

不允许在正则表达式中使用空字符类(非空字符类)

配置文件中的"extends": "eslint:recommended"属性启用此规则。

由于正则表达式中的空字符类不匹配任何内容,因此可能会输入错误。

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构造函数的字符串参数中报告空字符类。

此规则报告正确的代码时出现错误否定的示例:

/*eslint no-empty-character-class: "error"*/ var abcNeverMatches = new RegExp("^abc[]"

版本

这条规则是在ESLint 0.22.0中引入的。

资源