YouTube视频ID的正则表达式

时间:2017-02-24 14:05:59

标签: javascript regex

我有以下代码从字符串中检测视频ID。 它实际上捕获了我需要的更多情况,并且我想更简单地使用这种格式的字符串返回id:

https://www.youtube.com/watch?v=85PMSYAguZ8
https://youtu.be/85PMSYAguZ8

代码:

_getVideoIdFromUrl: function (value) {
var regEx = /^(?:https?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:embed\/|v\/|watch\?v=|watch\?.+&v=))((\w|-){11})(?:\S+)?$/;
var matches = value.match(regEx);
if (matches) {
    return matches[1];
}
return false;

},

我需要帮助来相应地改变regEx,任何想法?

目前它检测到:

YES   'http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel',
YES   'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub',
YES   'http://youtu.be/6dwqZw0j_jY',
YES   'http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be',    
YES   'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub',
YES   'http://www.youtube.com/embed/nas1rJpm7wY?rel=0',
YES   'http://www.youtube.com/watch?v=peFZbP64dsU',
YES   'http://youtube.com/v/dQw4w9WgXcQ?feature=youtube_gdata_player',
YES   'http://youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
YES   'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player',  
YES   'http://youtu.be/afa-5HQHiAs',
YES   'http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player'
NO    '//www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0',
NO    'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo',
NO    'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I',
NO    'http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY',
NO    'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo?rel=0',
NO    'http://www.youtube.com/watch?v=cKZDdG9FTKY&feature=channel',
NO    'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I',
NO    'http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player',
NO    'http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
NO    'http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player',
NO    'http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player',

2 个答案:

答案 0 :(得分:3)

试试这个:



urls = ['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://youtu.be/6dwqZw0j_jY',
'http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be',
'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub',
'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/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
'http://youtu.be/afa-5HQHiAs',
'http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player',
'//www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0',
'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo',
'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I',
'http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY',
'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/ytscreeningroom?v=NRHVzbJVx8I',
'http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player',
'http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
'http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player',
'http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player']

_getVideoIdFromUrl = function (value) {
  var regEx = "^(?:https?:)?//[^/]*(?:youtube(?:-nocookie)?\.com|youtu\.be).*[=/]([-\\w]{11})(?:\\?|=|&|$)";
  var matches = value.match(regEx);
  if (matches) {
      console.log(value + "\n" + matches[1] + "\n");
  }
  return false;
}

urls.forEach(function(url) {
  _getVideoIdFromUrl(url)
});




答案 1 :(得分:0)

我会做这样的事情:
它获取您指定的所有示例的视频ID



const urls = ['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://youtu.be/6dwqZw0j_jY',
'http://www.youtube.com/watch?v=6dwqZw0j_jY&feature=youtu.be',
'http://www.youtube.com/watch?v=yZ-K7nCVnBI&playnext_from=TL&videos=osPknwzXEas&feature=sub',
'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/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
'http://www.youtube.com/watch?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
'http://youtu.be/afa-5HQHiAs',
'http://youtu.be/dQw4w9WgXcQ?feature=youtube_gdata_player',
'//www.youtube-nocookie.com/embed/up_lNV-yoK4?rel=0',
'http://www.youtube.com/user/Scobleizer#p/u/1/1p3vcRhsYGo',
'http://www.youtube.com/ytscreeningroom?v=NRHVzbJVx8I',
'http://www.youtube.com/user/SilkRoadTheatre#p/a/u/2/6dwqZw0j_jY',
'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/ytscreeningroom?v=NRHVzbJVx8I',
'http://youtube.com/vi/dQw4w9WgXcQ?feature=youtube_gdata_player',
'http://youtube.com/?v=dQw4w9WgXcQ&feature=youtube_gdata_player',
'http://youtube.com/?vi=dQw4w9WgXcQ&feature=youtube_gdata_player',
'http://youtube.com/watch?vi=dQw4w9WgXcQ&feature=youtube_gdata_player']

const getVideoId = (url) => {
  if (/^(?:http:\/\/)?youtu\.be/.test(url)){
    return url.match(/^(?:http:\/\/)?youtu\.be\/(.{11})/)[1]
  } else if (/^(?:(?:http:)?\/\/)?(?:www\.)?youtube(?:-nocookie)?.com/.test(url)){
    let match = url.match(/(?:http:\/\/)?(?:www\.)?youtube\.com.*?v=(.{11})/)
    if (!match) match = url.match(/(?:vi=|vi?\/|embed\/)(.{11})/)
    if (!match && /user/.test(url)) {
      match = url.split('/')
      return match[match.length - 1].split('?')[0]
    }
    return (match || [,url])[1]
  } else return null
}

for (let url of urls){
  console.log(url)
  console.log(getVideoId(url))
}