6

In IE, "x".split(/(x)/).length returns 0

In Firefox, Chrome, Safari, and Opera, it returns 3.

Does anybody know the reason why? If possible, a reference link will be greatly appreciated.

I believe that it is a IE regex implementation issue, but I can't find any document about that.

YOU
  • 120,166
  • 34
  • 186
  • 219

3 Answers3

6

You're correct that there are implementation issues. IE both ignores empty values and capture blocks within regular expressions.

So for

"foo".split(/o/)

IE gives

[f]

where the other browsers give

["f","",""]

and when you add the capturing:

"foo".split(/(o)/)

IE performs the same, but the others add the captured delimiter to the resulting array to give

["f","o","","o",""]

So unfortunately you probably either need to avoid using split, or code around these issues.

Shaun
  • 1,478
  • 10
  • 9
3

Here for example http://blog.stchur.com/2007/03/28/split-broken-in-ie/

Patrick
  • 15,702
  • 1
  • 39
  • 39
1

I had the same problem with the broken IE implementation of split.

Here's a small library file that fixed the problem perfectly.

Larry K
  • 47,808
  • 15
  • 87
  • 140