Fork me on GitHub

JSLint Error Explanations

JSLint will hurt your feelings. It's time to make them better!


Empty class

When do I get this error?

JSLint and JSHint (prior to version 1.0.0) will throw the "Empty class" error when it encounters a regular expression literal containing an empty character class. Note that it will not raise this error for calls to the RegExp constructor. The following example defines a regular expression including an empty character class:

Why do I get this error?

The error is raised to highlight code that may not work as you expect it to. According to the regular expression grammar in the ECMAScript standard, empty character classes are allowed (ES5 A.7):

CharacterClass ::
    [ [lookahead ∉ {^}] ClassRanges ]
    [ ^ ClassRanges ]

ClassRanges ::
    [empty]
    NonemptyClassRanges

However, an empty character class can never match anything, meaning the regular expression in the example above will always fail to match. Since it's unlikely you intended such behaviour, a warning is raised to highlight the fact that you may have overlooked something, or simply made a small typo.

There is no JSLint or JSHint option that can be set to surpress this error. The best way to resolve it is to simply remove any empty character classes from the regular expressions in question:

However, if you really do need an empty character class, you can use the RegExp constructor to create your regular expression:


comments powered by Disqus