javascript str-match无法使用

时间:2013-08-17 21:17:00

标签: javascript preg-match match parse-url

我需要从字符串中解析url240和url360。但我不能这样做。在PHP上它非常简单:

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);

1 个答案:

答案 0 :(得分: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"]  */