1

I just came across an issue in IE8, using the .split() method.

element.name = 'question[10]';
var splitName = element.name.split(/\[([0-9]+)\]/);
// Split name just had 1 element, 
//    splitName[0] == 'question'
// In Chrome/Firefox it has 3
//    splitName[0] == 'question'
//    splitName[1] == '10'
//    splitName[2] == ''

I've found a work around using this code, but I'm curious if this is a bug with IE8, or if this is expected, and there is a better way to achieve the same result without needing to modify the split method.

joews
  • 29,767
  • 10
  • 79
  • 91
TMH
  • 6,096
  • 7
  • 51
  • 88
  • possible duplicate of [JavaScript: split doesn't work in IE?](http://stackoverflow.com/questions/1453521/javascript-split-doesnt-work-in-ie) – joews May 06 '15 at 14:43
  • If the names are in the format `question[n]` I would not use an RE at all ... – Alex K. May 06 '15 at 14:45

1 Answers1

1

This is expected (somewhat) behaviour.

From the MDN:

If separator is a regular expression that contains capturing parentheses, then each time separator is matched, the results (including any undefined results) of the capturing parentheses are spliced into the output array. However, not all browsers support this capability.)

Delete the parentheses from the regular expression and the results will almost be the same ( Chrome/FF take the notion of a 'separator' at the end of a string verbatim in producing another empty string ).

collapsar
  • 17,010
  • 4
  • 35
  • 61