Jquery regexp make dynamic url

I need regexp help, because for me it will take a lot of time, for you a few minutes :)

I have a url:

http://www.youtube.com/watch?v=9_Hd8hXhg7o&feature=youtube_gdata 

I cannot add this to the embed object, for embedding I have to change this URL:

http://www.youtube.com/v/9_Hd8hXhg7o&hl=en_US&fs=1&

This means that I want to add the youtube track to some kind of variable, something like this

var url = after regexp "9_Hd8hXhg7o";

"http://www.youtube.com/v/" + url +"&hl=en_US&fs=1&";

thank!

+3
source share
1 answer

This should do it:

var url = "http://www.youtube.com/watch?v=9_Hd8hXhg7o&feature=youtube_gdata";

var id = url.match(/(\?|&)v=(.*?)(&|$)/)[2];

var new_url = "http://www.youtube.com/v/" + id+"&hl=en_US&fs=1&";

This should work in almost all formats that you throw at it. This snippet searches for ?v=or &v=any characters after that before the character &or end of the line, so the identifier can also be found from them:

http://www.youtube.com/watch?v=9_Hd8hXhg7o
http://www.youtube.com/watch?feature=youtube_gdata&v=9_Hd8hXhg7o
http://www.youtube.com/watch?fs=1&v=9_Hd8hXhg7o&feature=youtube_gdata&
+4
source

Source: https://habr.com/ru/post/1739183/


All Articles