How do I get the YouTube video ID from a URL?

前端 未结 30 2392
北恋
北恋 2020-11-22 03:06

I want to get the v=id from YouTube’s URL with JavaScript (no jQuery, pure JavaScript).

Example YouTube URL formats

http://www.youtube.c

相关标签:
30条回答
  • 2020-11-22 03:44

    One more:

    var id = url.match(/(^|=|\/)([0-9A-Za-z_-]{11})(\/|&|$|\?|#)/)[2]
    

    It works with any URL showed in this thread.

    It won't work when YouTube addS some other parameter with 11 base64 characters. Till then it is the easy way.

    0 讨论(0)
  • 2020-11-22 03:45

    I made a small function to extract the video id out of a Youtube url which can be seen below.

    var videoId = function(url) {
       var match = url.match(/v=([0-9a-z_-]{1,20})/i);
       return (match ? match['1'] : false);
    };
    
    console.log(videoId('https://www.youtube.com/watch?v=dQw4w9WgXcQ'));
    console.log(videoId('https://www.youtube.com/watch?t=17s&v=dQw4w9WgXcQ'));
    console.log(videoId('https://www.youtube.com/watch?v=dQw4w9WgXcQ&t=17s'));

    This function will extract the video id even if there are multiple parameters in the url.

    0 讨论(0)
  • 2020-11-22 03:46

    I liked Surya's answer.. Just a case where it won't work...

    String regExp = "/.*(?:youtu.be\\/|v\\/|u/\\w/|embed\\/|watch\\?.*&?v=)";
    

    doesn't work for

    youtu.be/i4fjHzCXg6c  and  www.youtu.be/i4fjHzCXg6c
    

    updated version:

    String regExp = "/?.*(?:youtu.be\\/|v\\/|u/\\w/|embed\\/|watch\\?.*&?v=)";
    

    works for all.

    0 讨论(0)
  • 2020-11-22 03:47
    /^.*(youtu.be\/|v\/|e\/|u\/\w+\/|embed\/|v=)([^#\&\?]*).*/
    

    Tested on:

    • http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
    • http://www.youtube.com/embed/0zM3nApSvMg?rel=0
    • http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
    • http://www.youtube.com/watch?v=0zM3nApSvMg
    • http://youtu.be/0zM3nApSvMg
    • http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
    • http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/KdwsulMb8EQ
    • http://youtu.be/dQw4w9WgXcQ
    • http://www.youtube.com/embed/dQw4w9WgXcQ
    • http://www.youtube.com/v/dQw4w9WgXcQ
    • http://www.youtube.com/e/dQw4w9WgXcQ
    • http://www.youtube.com/watch?v=dQw4w9WgXcQ
    • http://www.youtube.com/?v=dQw4w9WgXcQ
    • http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ
    • http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ
    • http://www.youtube.com/user/IngridMichaelsonVEVO#p/u/11/KdwsulMb8EQ
    • http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0

    Inspired by this other answer.

    0 讨论(0)
  • 2020-11-22 03:47
    function parser(url){
        var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\/)|(\?v=|\&v=))([^#\&\?]*).*/;
        var match = url.match(regExp);
        if (match && match[8].length==11){
                alert('OK');
        }else{
                alert('BAD');
        }
    }
    

    For testing:

    https://www.youtube.com/embed/vDoO_bNw7fc - attention first symbol «v» in «vDoO_bNw7fc»
    
    http://www.youtube.com/user/dreamtheater#p/u/1/oTJRivZTMLs
    https://youtu.be/oTJRivZTMLs?list=PLToa5JuFMsXTNkrLJbRlB--76IAOjRM9b
    http://www.youtube.com/watch?v=oTJRivZTMLs&feature=youtu.be
    https://youtu.be/oTJRivZTMLs
    http://youtu.be/oTJRivZTMLs&feature=channel
    http://www.youtube.com/ytscreeningroom?v=oTJRivZTMLs
    http://www.youtube.com/embed/oTJRivZTMLs?rel=0
    http://youtube.com/v/oTJRivZTMLs&feature=channel
    http://youtube.com/v/oTJRivZTMLs&feature=channel
    http://youtube.com/vi/oTJRivZTMLs&feature=channel
    http://youtube.com/?v=oTJRivZTMLs&feature=channel
    http://youtube.com/?feature=channel&v=oTJRivZTMLs
    http://youtube.com/?vi=oTJRivZTMLs&feature=channel
    http://youtube.com/watch?v=oTJRivZTMLs&feature=channel
    http://youtube.com/watch?vi=oTJRivZTMLs&feature=channel
    
    0 讨论(0)
  • 2020-11-22 03:47

    We know these characters "?v=" can never appear more than ones but 'v' can appear somehow in the Id Itself so we use "?v=" as delimiter. See it Working Here

    //Get YouTube video Id From Its Url
    
         $('button').bind('click',function(){
    var 
    url='http://www.youtube.com/watch?v=u8nQa1cJyX8',
    videoId = url.split('?v='),//Split data to two
    YouTubeVideoId=videoId[1];
    alert(YouTubeVideoId);return false;
    
    });
    
    <button>Click ToGet VideoId</button>
    
    0 讨论(0)
提交回复
热议问题