Fork me on GitHub

JSLint Error Explanations

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

A regular expression literal can be confused with '/='

When do I get this error?

JSLint and JSHint (prior to version 1.0.0) will throw the "A regular expression literal can be confused with '/='" error when they encounter a regular expression literal that begins with the = character. In the following example we attempt to assign a regular expression literal to match the string "=1" to the variable x:

Why do I get this error?

This error is raised to highlight a potentially confusing piece of code. Your code will run fine if you do not fix this error, but it may be confusing to others, especially at first glance to someone quickly searching through your script.

The / character is ambiguous in JavaScript. It can either signify the start or end of a regular expression literal, as it does in the example above, or it can be interpreted as the division operator. Like most of the arithmetic operators, the division operator can be combined with the assignment operator to produce a shorthand:

This ambiguity is not a problem because the parser should always be able to differentiate between the two uses. However, you can see why the regular expression example at the top of this page could cause initial confusion.

To solve this issue, you can simply escape the = character in the regular expression. This will behave in exactly the same way but since the = character is no longer the first, the error is not raised:

Alternatively, you can use the RegExp constructor, which removes the need for the delimiting / characters:

James Allardice

This article was written by James Allardice, an enthusiastic young JavaScript developer at Global Personals (we're looking for developers so please apply). He is passionate about writing clean, maintainable JavaScript and uses JSHint every day to help achieve this.

You can follow him on Twitter, fork him on GitHub and find him on Google+.

Proceeds generated by this site are donated to help advance other open source projects

comments powered by Disqus