disallow empty character classes in regular expressions (no-empty-character-class)

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.

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.

Resources

Js中文网,专注分享前端最新技术、大厂面试题、聊点程序员轶事、职场感悟,做前端技术的传播者.

加入前端布道师交流群

扫描二维码回复 加群 学习,与大厂大佬讨论技术.

BAT面试题大全