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

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

提交回复
热议问题