2013-08-17 66 views
0

我需要从字符串中解析url240和url360。但我不能这样做。在PHP这是很容易的:javascript str-match无法使用

preg_match('/&url240=(.*?)&/mis', $string, $C); 

但我不能这样做的JavaScript。我的javascript代码:

var str = "&url240=http://cs506410v4.vk.me/u170785079/videos/d10bdfccf6.240.mp4&url360=http://cs506410v4.vk.me/u170785079/videos/d10bdfccf6.360.mp4&url480="; 
var n=str.match(/url240=/gi); 
alert(n); 
+0

'alert()'说什么? – Pointy

+0

在Chrome和Firefox中,我使用该代码获得了'url240 ='。那是错的吗? – 2013-08-17 21:19:56

+0

为什么对这个问题downvote? –

回答

1

看起来你真的想使用exec而不是match,这样你就可以得到您的捕获组

var e = /&url240=(.*?)&/i.exec(str); 
e[1]; // "http://cs506410v4.vk.me/u170785079/videos/d10bdfccf6.240.mp4" 

如果你想使用exec找到多的事情,你可以把它放在一个循环中,例如

var re = /(.)/g, 
    str = '123', 
    e; 
while (e = re.exec(str)) console.log(e); 
/* ["1", "1", index: 0, input: "123"] 
    ["2", "2", index: 1, input: "123"] 
    ["3", "3", index: 2, input: "123"] */