Pregunta

I need to parse url240 and url360 from a string. But i cant do it. On PHP it is very easy:

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

But I cant do it on javascript. My javascript code:

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);
¿Fue útil?

Solución

It looks like you actually want to use exec and not match, so you can get your capture group

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

If you want to find multiple things using exec, you can put it in a loop, for example

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"]  */
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top