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

前端 未结 30 2937
北恋
北恋 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

    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];
    }
    

提交回复
热议问题