Regex Split bug in JScript?

I’ve been messing around with JavaScript quite a bit recently and I came across a ‘bug’ in the Microsoft implementation of the split() method.

Take the following example string (note the two tabs at the end):

var s = “HellotHowtaretyoutt”;

and then run the following to write it out to the screen:

document.writeln(s.split(“t”));

Well that works as expected, and outputs “Hello,How,are,you,,”;

But if you replace the string in the split method with an inline Regex object it fails if run under a Microsoft implementation

document.writeln(s.split(/t/));

This outputs the same as above in Firefox etc, but if run in JScript (i.e. Internet Explorer) you lose all empty elements and get the following: “Hello,How,are,you”. This is really weird, because if you use the string delimiter above it works just fine.

I have managed to find another blog post that tries to fix this problem, but doesn’t say why it happens.

http://blog.stevenlevithan.com/archives/cross-browser-split

To test this online, try going to W3Schools.com.

Leave a Reply

Your email address will not be published. Required fields are marked *