Kae Verens

ie6 bug – regexps, split, and carriage returns

by kae verens on Mar.16, 2005, under javascript

This one had me stumped for half an hour… In my AJAX shopping cart, I return a file which is delimited by carriage returns. Some of the lines of the file may be blank. On the client side, I then split that into an array. Firefox was giving me the correct array, but IE6 was dropping all the blank lines.

It took me a while, but I finally figured it out. It seems that if you split using the method file.split(/\n/), then IE6 rips up that regexp and replaces it with file.split(/\n+/).

Here is some sample code – this works perfectly in Firefox, but the problem is demonstrated in the first three alerts for IE6.

alert('one\ntwo\n\nthree'.split(/\n/));
alert('one\ntwo\n\n\nthree'.split(/\n/));
alert('one\ntwo\n\n\n\nthree'.split(/\n/));
alert('one\ntwo\n\nthree'.split('\n'));

As you can see, the solution is to split on the string '\n' instead of the regexp /\n/, although that obviously is not ideal in all cases.

As usual, we web developers can thank Microsoft for the hell that we go through.


2 Comments for this entry

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Blogroll

A few highly recommended websites...