匹配string中的所有URL并在JavaScript中返回数组

例如,我有以下字符串:

var string = 'watch this video http://vimeo.com/8122132 and then see this picture http://www.flickr.com/photos/pmorgan/32606683/'; 

我希望找到所有有效的URL并将它们放在一个数组中,用JavaScript(和jQuery)完成,所以在这种情况下:

 url[0] = http://vimeo.com/8122132 url[1] = http://www.flickr.com/photos/pmorgan/32606683/ 

目前,我只能匹配一个url,但我想匹配所有url。 这就是我所拥有的:

 geturl = new RegExp("(^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))"); var url = geturl.exec(string); $('#urls').html(url[0]); 

相信我,把url[1],url[2]等不起作用:(

有任何想法吗?

在Regexp中传递“g”

 geturl = new RegExp( "(^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal):(([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]))" ,"g" ); string.match(geturl).length 2 string.match(geturl) http://vimeo.com/8122132, http://www.flickr.com/photos/pmorgan/32606683/