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

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

提交回复
热议问题