Tomcat seems to eat slashes, though the only reference I can find to it is from 7 years ago.
I recently ran into an issue with a regex inside of a JSP file, with Tomcat in front of it. The JavaScript looked like this:
var regex = /^\\$|,/g;
The goal of it is to globally strip out all commas and dollar signs from a string, for display purposes only (no round-trip to the server here). It worked perfectly in all test & production environments.
Fast-forward several months since that code went live, and I’m splitting all the js out of the JSP. Suddenly that regex is broken; it strips out the commas, but not the dollar sign.
I went back over my steps, making sure I didn’t break anything along the way, but I didn’t. I inserted breakpoints, started logging everything in Firebug…yep, everything is fine beyond the call to the regex, but for some reason the regex isn’t working. I remove the extra slash, and everything is fine again.
But why?
I look at the code in QA & Production, and we have two slashes in the JSP. Then I view the source of the page in Firefox, and there’s only one slash in the source. Thinking my eyes are playing tricks on me, I hop over to Chrome, which does the same. Ditto IE, Safari, and Opera.
Turns out Tomcat is eating that extra slash in the JSP, resulting in the code we wanted to use all along, var regex = /^\$|,/g;, being sent to the browser. Once I split the code out of the JSP, Tomcat stopped chowing down and the extra slash was being interpreted by the browser, resulting in a non-functional regex. A quick tweak to the JavaScript, and all was right with the world.
This is highly annoying and seemingly undocumented behavior though. I shouldn’t need to worry that Tomcat is going to mangle my JavaScript before it hits the browser. Several months ago, I must have coded around the issue, not even realizing what was going on. If anyone finds an answer as to why Tomcat is doing this (besides it just being a bug), I’d love to hear it.
