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.
March 31st, 2005 on 1:35 pm
split isnt supposed to be used with regular expressions?
March 31st, 2005 on 1:41 pm
yes it is.
http://javascript.aernia.com/CoreReferenceJS15/string.html#1194452