javascript regex split not cross browser -


i trying split string 3 keywords so:

var option=$(this).text().split(/(to:|from:|line:)/); 

$(this).text() testing purposes "from:circular quay to:manly line:f1 manly"

all "normal" browsers return array of 7 elements ie7 & 8 returns array of 3 elements.

any ideas?

regex spliting doesn't work same in browsers, described this article.

  • internet explorer excludes empty values resulting array (e.g., when 2 delimiters appear next each other in data, or when delimiter appears @ start or end of data). doesn't make sense me, since ie include empty values when using string delimiter.

  • internet explorer , safari not splice values of capturing parentheses returned array (this functionality can useful simple parsers, etc.)

  • firefox not splice undefined values returned array result of non-participating capturing groups.

  • internet explorer, firefox, , safari have various additional edge-case bugs not follow split specification (which quite complex).

(note : behaviors changed in recent browsers, don't rely on these descriptions implementing browser specific algorithms!)

the news you, if order of delimiters same : interests you, content strings, here in browsers. have test each string of array see if it's empty string or delimiter.

if string has delimiters in same order, might want strip them in browsers not capturing them :

var s = s.split(/to:|from:|line:/); 

if need delimiters because order may change, you'd better split on \b , check strings.


Comments