我想要得到v=id从YouTube的URL与JavaScript(没有jQuery,纯JavaScript)。

YouTube URL格式示例

http://www.youtube.com/watch?v=u8nQa1cJyX8&a=GxdCwVVULXctT2lYDEPllDR0LRTutYfW

http://www.youtube.com/watch?v=u8nQa1cJyX8

或在URL中包含视频ID的任何其他YouTube格式。

这些格式的结果

u8nQa1cJyX8


当前回答

稍微严格一点的版本:

^https?://(?:www\.)?youtu(?:\.be|be\.com)/(?:\S+/)?(?:[^\s/]*(?:\?|&)vi?=)?([^#?&]+)

测试:

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

其他回答

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

最短高效

截至2015年1月1日,这些都不能在厨房里工作,特别是没有http/s协议和youtube-nocookie域的url。这里有一个修改过的版本,适用于所有这些不同的Youtube版本:

// Just the regex. Output is in [1]. /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/ // For testing. var urls = [ 'https://youtube.com/shorts/dQw4w9WgXcQ?feature=share', '//www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0', 'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo', 'http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel', 'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub', 'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I', 'http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY', 'http://youtu.be/6dwqZw0j_jY', 'http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be', 'http://youtu.be/afa-5HQHiAs', 'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo?rel=0', 'http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel', 'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub', 'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I', 'http://www.youtube.com/embed/nas1rJpm7wY?rel=0', 'http://www.youtube.com/watch?v=peFZbP64dsU', 'http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player', 'http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player', 'http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 'http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player', 'http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player', 'http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player', 'http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player' ]; var i, r, rx = /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/|shorts\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/; for (i = 0; i < urls.length; ++i) { r = urls[i].match(rx); console.log(r[1]); }

我发现最好的解决方案(从2019年到2021年)是:

function YouTubeGetID(url){
   url = url.split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
   return (url[2] !== undefined) ? url[2].split(/[^0-9a-z_\-]/i)[0] : url[0];
}

我在这里找到的。

/*
* Tested URLs:
var url = 'http://youtube.googleapis.com/v/4e_kz79tjb8?version=3';
url = 'https://www.youtube.com/watch?feature=g-vrec&v=Y1xs_xPb46M';
url = 'http://www.youtube.com/watch?feature=player_embedded&v=Ab25nviakcw#';
url = 'http://youtu.be/Ab25nviakcw';
url = 'http://www.youtube.com/watch?v=Ab25nviakcw';
url = '<iframe width="420" height="315" src="http://www.youtube.com/embed/Ab25nviakcw" frameborder="0" allowfullscreen></iframe>';
url = '<object width="420" height="315"><param name="movie" value="http://www.youtube-nocookie.com/v/Ab25nviakcw?version=3&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/Ab25nviakcw?version=3&amp;hl=en_US" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
url = 'http://i1.ytimg.com/vi/Ab25nviakcw/default.jpg';
url = 'https://www.youtube.com/watch?v=BGL22PTIOAM&feature=g-all-xit';
url = 'BGL22PTIOAM';
*/

这里有一个红宝石版本:

def youtube_id(url)
   # Handles various YouTube URLs (youtube.com, youtube-nocookie.com, youtu.be), as well as embed links and urls with various parameters
   regex = /(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|vi|e(?:mbed)?)\/|\S*?[?&]v=|\S*?[?&]vi=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/
   match = regex.match(url)
   if match && !match[1].nil?
      match[1]
   else
      nil
   end
end

测试方法:

example_urls = [
   'www.youtube-nocookie.com/embed/dQw4-9W_XcQ?rel=0',
   'http://www.youtube.com/user/Scobleizer#p/u/1/dQw4-9W_XcQ',
   'http://www.youtube.com/watch?v=dQw4-9W_XcQ&feature=channel',
   'http://www.youtube.com/watch?v=dQw4-9W_XcQ&playnext_from=TL&videos=osPknwzXEas&feature=sub',
   'http://www.youtube.com/ytscreeningroom?v=dQw4-9W_XcQ',
   'http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/dQw4-9W_XcQ',
   'http://youtu.be/dQw4-9W_XcQ',
   'http://www.youtube.com/watch?v=dQw4-9W_XcQ&feature=youtu.be',
   'http://youtu.be/dQw4-9W_XcQ',
   'http://www.youtube.com/user/Scobleizer#p/u/1/dQw4-9W_XcQ?rel=0',
   'http://www.youtube.com/watch?v=dQw4-9W_XcQ&playnext_from=TL&videos=dQw4-9W_XcQ&feature=sub',
   'http://www.youtube.com/ytscreeningroom?v=dQw4-9W_XcQ',
   'http://www.youtube.com/embed/dQw4-9W_XcQ?rel=0',
   'http://www.youtube.com/watch?v=dQw4-9W_XcQ',
   'http://youtube.com/v/dQw4-9W_XcQ?feature=youtube_gdata_player',
   'http://youtube.com/vi/dQw4-9W_XcQ?feature=youtube_gdata_player',
   'http://youtube.com/?v=dQw4-9W_XcQ&feature=youtube_gdata_player',
   'http://www.youtube.com/watch?v=dQw4-9W_XcQ&feature=youtube_gdata_player',
   'http://youtube.com/?vi=dQw4-9W_XcQ&feature=youtube_gdata_player',
   'http://youtube.com/watch?v=dQw4-9W_XcQ&feature=youtube_gdata_player',
   'http://youtube.com/watch?vi=dQw4-9W_XcQ&feature=youtube_gdata_player',
   'http://youtu.be/dQw4-9W_XcQ?feature=youtube_gdata_player'
]

# Test each one
example_urls.each do |url|
   raise 'Test failed!' unless youtube_id(url) == 'dQw4-9W_XcQ'
end

要查看此代码并在在线repl中运行测试,您也可以到这里: https://repl.it/@TomChapin/youtubeid

正如webstrap在评论中提到的:

如果视频以“v”开头,并且它来自youtube .be,它就可以工作 正则表达式包含一个小错误\??v?=?这应该在 注意部分,否则如果id以a开头,您将过滤'v' “v”。这应该能解决问题 / ^。* ((youtu.be \ /) | (v \ /) | (\ w / u \ / \ \ /) |(嵌入\ /)|(看\ ? ? v ?=?))([^#\&\?]*).*/