9

From what I've tested

"aba".split(/a/).length

returns

  • 1 in ie8
  • 3 in firefox, chrome, opera

I was always prepared to handle differences in DOM manipulation, or Events model, but I've thought that things like strings, regexps, etc. are well defined. Was I wrong?

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
qbolec
  • 5,374
  • 2
  • 35
  • 44
  • Good question. _"Was I wrong?"_ - Evidently you were. But you're not the only one who's been caught out by this. When I tried IE9 it returned `3` but only when not running in IE7 or IE8 document mode... – nnnnnn Nov 23 '12 at 07:59
  • I was going to blame IE8, but IE9 (in IE9 mode in an empty document) returns `1` as well. – John Dvorak Nov 23 '12 at 08:00
  • 3
    Related: http://stackoverflow.com/questions/1453521, http://stackoverflow.com/questions/6422355 and http://blog.stevenlevithan.com/archives/cross-browser-split – Tomasz Nurkiewicz Nov 23 '12 at 08:01
  • Apparently you were wrong. Question, though: Is this just a example or regex usage in `string.split`? Because just splitting on `"a"` would be more efficient in this case. – Cerbrus Nov 23 '12 at 08:02
  • @Cerbrus: there is no invalidity in using regular expressions for splitting. – GottZ Nov 23 '12 at 08:06
  • 1
    @Jan-StefanJanetzky: I didn't say that. But if you're doing a simple split on -one- character, using a regex is kindof overkill. – Cerbrus Nov 23 '12 at 08:09

1 Answers1

4

IE removes from the split result array all undefined or empty strings.

As your question seems to be about the existence of a standard, then EcmaScript is the best match in the Javascript world.

And the behavior of split on regex is documented : http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4.14

As it obvious from the example, empty strings should not be removed from the resulting array, so IE (as suspected) is faulty.

"A<B>bold</B>and<CODE>coded</CODE>".split(/<(\/)?([^<>]+)>/)

    evaluates to the array

["A", undefined, "B", "bold", "/", "B", "and", undefined,  "CODE", "coded", "/", "CODE", ""]

In fact, there are other differences between browsers. A solution could be to use a cross-browser split regex script but it's probably better to be simply aware of the differences and handle with proper tests the array returned by split. Or use some tricks.

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758