Browsers too leniant with regular expressions

Just had an interesting issue pop up that I thought I’d share. We should all know by now that sometimes browsers are way too forgiving about the bad code we write. They insert HTML elements when tags aren’t closed properly and generally try to “help” developers as much as possible. This behavior also invades the JavaScript space where automatic semicolon insertion reigns supreme. Last night I discovered another one of these strange issues. Consider the following regular expression:

var re = /blah[/]blah/;

Now, to most people who’ve written regular expressions before, you know that [/] is invalid because the forward slash is a metacharacter which must be escaped for use in a regular expression. This should cause a syntax error, however, every browser allows this an interprets it as if the code where [/].

I discovered this last night when running some code through the YUI Compressor, which uses Rhino to parse the code before outputting a compressed version. This resulted in a syntax error that stopped the compression process. After some searching, I figured out this was the problem.

In my opinion, it’s browser behavior like this that makes for crappy web development practices. The more the browsers “forgive” developer mistakes, the less important it is to write good code. If there’s no benefit to writing good code, people won’t do it.

Understanding JavaScript Promises E-book Cover

Demystify JavaScript promises with the e-book that explains not just concepts, but also real-world uses of promises.

Download the Free E-book!

The community edition of Understanding JavaScript Promises is a free download that arrives in minutes.