JS.BASE.NO.EMPTY.CHARACTER.CLASS

Disallow empty character classes in regular expressions

Because empty character classes in regular expressions do not match anything, they might be typing mistakes.

Copy
var foo = /^abc[]/;

Rule Details

This rule disallows empty character classes in regular expressions.

Examples of incorrect code for this rule:

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

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

Examples of correct code for this rule:

Copy
/*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:

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

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

The content on this page is adapted from the ESLint User Guide. Copyright © OpenJS Foundation and other contributors, www.openjsf.org. All rights reserved. https://eslint.org/docs/rules/