Unclosed regular expression

When do I get this error?

The "Unclosed regular expression" error is thrown when JSLint or JSHint encounters a regular expression literal with no closing / character. Here's an example:

var regex = /^unclosed$;

Why do I get this error?

This error is raised to highlight a fatal JavaScript syntax error. Your code will not run unless you fix this issue. The ECMAScript 5 specification lists the following grammar for regular expression literals (ES5 §7.8.5):

RegularExpressionLiteral ::
    / RegularExpressionBody / RegularExpressionFlags

This production makes it clear that regular expression literal bodies must be terminated by a / character. Not doing so will always cause a syntax error. To fix this issue, simply close the regular expression in question:

var regex = /^closed$/;

In JSHint 1.0.0 and above you have the ability to ignore any warning with a special option syntax. Since this message relates to a fatal syntax error you cannot disable it.


About the author

James Allardice

This article was written by James Allardice, Software engineer at Tesco and orangejellyfish in London. Passionate about React, Node and writing clean and maintainable JavaScript. Uses linters (currently ESLint) every day to help achieve this.