views:

41

answers:

1

I am trying to split a string in Javascript using a Regular Expression. My code is as follows:

var status = "This is a test http://yfrog.com/5y6eruj";    
var regexp = /(http:\/\/yfrog\.com(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?)/gi;  
matchParts = status.split(regexp);
alert(matchParts);

In Chrome and FF when I alert matchParts it is an array containing the text and then the url. In IE however matchParts is just the text and the url has vanished!

Either there is an odd bug in IE or my Regular Expression isn't quite right...please help!

+4  A: 

I would not use "split" for this, because it's buggy: http://blog.stevenlevithan.com/archives/cross-browser-split

Try

var matchParts = regexp.exec(status);

instead. You may have to tinker with the regex a bit (I'll try it and update).

edit If you add (.*) to the beginning of the regular expression, you'll pick up the leading text too.

Pointy
That is great, thanks! Using exec() works in IE :)
Dan Millar