Javascript regular expression for extracting Youtu

2019-02-20 02:20发布

问题:

The following code is used to get Youtube video ids in order to get a thumbnail image.

What is the reasoning behind the first regular expression and what is it doing exactly? It appears to be returning at least two results. Also, could the two be combined?

else if(url.match("youtube.com/")){

    var vid;
    var results;

    //http://www.youtube.com/watch?v=GItD10Joaa0
    results = url.match("[\\?&]v=([^&#]*)");

    vid = ( results === null ) ? url : results[1];

    return "http://img.youtube.com/vi/"+vid+"/2.jpg";
} else if( url.match("youtu.be/") ) {

    var vid;
    var results;

    // http://youtu.be/5uxd-521uus?hd=1
    // results = url.match("[^http://youtu.be/](.*)[^?hd=1]");
    // Corrected
    results = url.match(""^http://youtu.be/(.*)(?=hd=1)");

    //alert(results[0]);
    vid = ( results === null ) ? url : results[0];

    return "http://img.youtube.com/vi/"+vid+"/2.jpg";
}

回答1:

"[\\?&]v=([^&#]*)"

explained (after reduction from a JavaScript string to a regex):

[\?&]   # Match a ? or a & (the backslash is unnecessary here!)
v=      # Match the literal text "v="
(       # Capture the following into backreference no. 1:
 [^&#]* # Zero or more characters except & or #
)       # End of capturing group.

The second regex [^http://youtu.be/](.*)[^?hd=1] is very wrong.

It probably should read

"^http://youtu.be/(.*)(?=hd=1)"


回答2:

If you are referring to...

results = url.match("[\\?&]v=([^&#]*)");

Then it is matching a literal \, ? or & followed by literal v= followed by a capturing group which is capturing 0 or more of any characters that are not & or #.



回答3:

The 1st regex is checking for "?v=GItD10Joaa0" when the url is something like "youtube.com/" and the 2nd is checking for "www.youtube.com/index?feature=youtu.be" when the url is "http://www.youtube.com/index?feature=youtu.be"

So you can simply use the 1st regex if you want to get ids from 1st url and likewise :)



回答4:

Ok, I did some fishing around and came accross this regex. It should suit the purpose described above.

youtu(?:\.be|be\.com)/(?:.*v(?:/|=)|(?:.*/)?)([a-zA-Z0-9-_]+)

From: C# regex to get video id from youtube and vimeo by url

And: http://forrst.com/posts/Automatic_YouTube_Thumbnails_with_PHP_and_Regex-uta