Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE8 parses this simple regex differently from all other browsers

I am trying to use this function to create 2 results from value

function split(val){
  return val.split( /,\s*/ );
};
value = "Jim, ";
var terms = split( value );

terms;

All other browsers including IE9, will produce terms = ["Jim", ""]

However, IE8 and probably IE7 produces this : terms = ["Jim"]

Does anyone have any suggestions or alternatives that could possibly work for IE8 ?

like image 442
Trip Avatar asked Jun 21 '12 18:06

Trip


1 Answers

You might be better off going with:

val.split(',')

This seems to work consistently in all browsers.

Any trailing whitespace after the commas still has to be stripped off afterwards. Something along the lines of:

for (var i = 0; i < terms.length; i++) {
    terms[i] = terms[i].replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

Apparently, in IE8 and earlier, empty-string matches are ignored by split() when a regex parameter is used. A string parameter works fine:

'axx'.split('x')    // All browsers: ["a", "", ""]
'axx'.split(/x/)    // IE6/7/8: ["a"], all other browsers: ["a", "", ""]
like image 181
Matt Coughlin Avatar answered Oct 15 '22 22:10

Matt Coughlin