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.

Disclaimer: Any viewpoints and opinions expressed in this article are those of Nicholas C. Zakas and do not, in any way, reflect those of my employer, my colleagues, Wrox Publishing, O'Reilly Publishing, or anyone else. I speak only for myself, not for them.

Both comments and pings are currently closed.