ie6 bug – regexps, split, and carriage returns

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.

  1. split isnt supposed to be used with regular expressions?

%d bloggers like this: