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

前端 未结 30 2391
北恋
北恋 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:36

    You don't need to use a regular expression for this.

    var video_id = window.location.search.split('v=')[1];
    var ampersandPosition = video_id.indexOf('&');
    if(ampersandPosition != -1) {
      video_id = video_id.substring(0, ampersandPosition);
    }
    
    0 讨论(0)
  • 2020-11-22 03:37

    Chris Nolet cleaner example of Lasnv answer is very good, but I recently found out that if you trying to find your youtube link in text and put some random text after the youtube url, regexp matches way more than needed. Improved Chris Nolet answer:

    /^.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]{11,11}).*/

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

    tl;dr.

    Matches all URL examples on this question and then some.

    let re = /^(https?:\/\/)?((www\.)?(youtube(-nocookie)?|youtube.googleapis)\.com.*(v\/|v=|vi=|vi\/|e\/|embed\/|user\/.*\/u\/\d+\/)|youtu\.be\/)([_0-9a-z-]+)/i;
    let id = "https://www.youtube.com/watch?v=l-gQLqv9f4o".match(re)[7];
    

    ID will always be in match group 7.

    Live examples of all the URLs I grabbed from the answers to this question: https://regexr.com/3u0d4

    Full explanation:

    As many answers/comments have brought up, there are many formats for youtube video URLs. Even multiple TLDs where they can appear to be "hosted".

    You can look at the full list of variations I checked against by following the regexr link above.

    Lets break down the RegExp.

    ^ Lock the string to the start of the string. (https?:\/\/)? Optional protocols http:// or https:// The ? makes the preceding item optional so the s and then the entire group (anything enclosed in a set of parenthesis) are optional.

    Ok, this next part is the meat of it. Basically we have two options, the various versions of www.youtube.com/...[id] and the link shortened youtu.be/[id] version.

    (                                                  // Start a group which will match everything after the protocol and up to just before the video id.
      (www\.)?                                         // Optional www.
      (youtube(-nocookie)?|youtube.googleapis)         // There are three domains where youtube videos can be accessed. This matches them.
      \.com                                            // The .com at the end of the domain. 
      .*                                               // Match anything 
      (v\/|v=|vi=|vi\/|e\/|embed\/|user\/.*\/u\/\d+\/) // These are all the things that can come right before the video id. The | character means OR so the first one in the "list" matches.
      |                                                // There is one more domain where you can get to youtube, it's the link shortening url which is just followed by the video id. This OR separates all the stuff in this group and the link shortening url.
      youtu\.be\/                                      // The link shortening domain
    )                                                  // End of group
    

    Finally we have the group to select the video ID. At least one character that is a number, letter, underscore, or dash.

    ([_0-9a-z-]+)
    

    You can find out much more detail about each part of the regex by heading over the regexr link and seeing how each part of the expression matches with the text in the url.

    0 讨论(0)
  • 2020-11-22 03:39
    var video_url = document.getElementById('youtubediv').value;
            if(video_url!=""){
            ytid(video_url);
            document.getElementById("youtube").setAttribute("src","http://www.youtube.com/embed/"+ytid(video_url));
            }
            function ytid(video_url){
                var video_id = video_url.split('v=')[1];
                var ampersandPosition = video_id.indexOf('&');
                if(ampersandPosition != -1) {
                    video_id = video_id.substring(0, ampersandPosition);
                }
                return video_id;
            }
    

    i hope it can help

    0 讨论(0)
  • 2020-11-22 03:40
    function youtube_parser(url){
        var match = url.match(/^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/);
        return (match&&match[7].length==11)?match[7]:false;
    }
    

    Shortest and Efficient

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

    I have summed up all the suggestions and here is the universal and short answer to this question:

    if(url.match('http://(www.)?youtube|youtu\.be')){
        youtube_id=url.split(/v\/|v=|youtu\.be\//)[1].split(/[?&]/)[0];
    }
    
    0 讨论(0)
提交回复
热议问题