Javascript String.split() function behaviour

In one of my recent projects I had the following line of Javascript to split up a string based on opening "(" and closing ")" brackets.

"Hello(World)".split(/(\(|\))/)
I found that in Firefox 3, the split command would return the array: [Hello,(,World,),]
However in IE7 I would get this: [Hello,World,]

For interest sake I also tried it in Chrome and it does what Firefox does. This is one time that IE did what I expected while FF and Chrome seemed to stumble.

I eventually changed the regex to the following to get all the browsers to produce the same result (i.e. the one IE7 originally produced).
"Hello(World)".split(/\(|\)/)


About this entry


0 comments: