我想要得到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


当前回答

试试这个——

function getYouTubeIdFromURL($url) 
{
  $pattern = '/(?:youtube.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu.be/)([^"&?/ ]{11})/i';
  preg_match($pattern, $url, $matches);

  return isset($matches[1]) ? $matches[1] : false;
}

其他回答

我把Lasnv的回答简化了一点。

它还修复了WebDeb描述的错误。

下面就是:

var regExp = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
var match = url.match(regExp);
if (match && match[2].length == 11) {
  return match[2];
} else {
  //error
}

下面是一个regexer链接: http://regexr.com/3dnqv

/^.*(youtu.be\/|v\/|e\/|u\/\w+\/|embed\/|v=)([^#\&\?]*).*/

测试:

http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 http://www.youtube.com/embed/0zM3nApSvMg?rel=0 http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index http://www.youtube.com/watch?v=0zM3nApSvMg http://youtu.be/0zM3nApSvMg http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/KdwsulMb8EQ http://youtu.be/dQw4w9WgXcQ http://www.youtube.com/embed/dQw4w9WgXcQ http://www.youtube.com/v/dQw4w9WgXcQ http://www.youtube.com/e/dQw4w9WgXcQ http://www.youtube.com/watch?v=dQw4w9WgXcQ http://www.youtube.com/?v=dQw4w9WgXcQ http://www.youtube.com/watch?feature=player_embedded&v=dQw4w9WgXcQ http://www.youtube.com/?feature=player_embedded&v=dQw4w9WgXcQ http://www.youtube.com/user/IngridMichaelsonVEVO#p/u/11/KdwsulMb8EQ http://www.youtube-nocookie.com/v/6L3ZvIMwZFM?version=3&hl=en_US&rel=0

受到另一个答案的启发。

虽然说得太晚了,但我已经把mantish和j-w的两个精彩回答搞混了。首先,修改后的正则表达式:

const youtube_regex = /^.*(youtu\.be\/|vi?\/|u\/\w\/|embed\/|\?vi?=|\&vi?=)([^#\&\?]*).*/

下面是测试代码(我已经将mantish的原始测试用例添加到j-w的更糟糕的测试用例中):

 var urls = [
      'http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index',
      'http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o',
      'http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0',
      'http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s',
      'http://www.youtube.com/embed/0zM3nApSvMg?rel=0',
      'http://www.youtube.com/watch?v=0zM3nApSvMg',
      'http://youtu.be/0zM3nApSvMg',
      '//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 failures = 0;
  urls.forEach(url => {
    const parsed = url.match(youtube_regex);
    if (parsed && parsed[2]) {
      console.log(parsed[2]);
    } else {
      failures++;
      console.error(url, parsed);
    }
  });
  if (failures) {
    console.error(failures, 'failed');
  }

实验版处理评论中提到的m.b utube网址:

const youtube_regex = /^.*((m\.)?youtu\.be\/|vi?\/|u\/\w\/|embed\/|\?vi?=|\&vi?=)([^#\&\?]*).*/

它需要在测试中的两个地方将已解析的[2]更改为已解析的[3](然后通过将m.u utube urls添加到测试中)。如果你发现问题请告诉我。

Java代码:(适用于所有url:

http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o http://youtube.googleapis.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0 http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s http://www.youtube.com/embed/0zM3nApSvMg?rel=0” http://www.youtube.com/watch?v=0zM3nApSvMg http://youtu.be/0zM3nApSvMg http://www.youtube.com/watch?v=0zM3nApSvMg/ http://www.youtube.com/watch?feature=player_detailpage&v=8UVNT4wvIGY

)

    String url = "http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index";

    String regExp = "/.*(?:youtu.be\\/|v\\/|u/\\w/|embed\\/|watch\\?.*&?v=)";
    Pattern compiledPattern = Pattern.compile(regExp);
    Matcher matcher = compiledPattern.matcher(url);
    if(matcher.find()){
        int start = matcher.end();
        System.out.println("ID : " + url.substring(start, start+11));

    }

DailyMotion网站:

String url = "http://www.dailymotion.com/video/x4xvnz_the-funny-crash-compilation_fun";

    String regExp = "/video/([^_]+)/?";
    Pattern compiledPattern = Pattern.compile(regExp);
    Matcher matcher = compiledPattern.matcher(url);
    if(matcher.find()){
        String match = matcher.group();
        System.out.println("ID : " + match.substring(match.lastIndexOf("/")+1));

    }

这个短片适用于我尝试过的每个youtube链接。

url.match(/([a-z0-9_-]{11})/gim)[0]

https://regexr.com/3nsop