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

ID : 10138

viewed : 28

Tags : javascriptregexurlyoutubejavascript

Top 5 Answer for javascript - How do I get the YouTube video ID from a URL?

vote vote

90

I made an enhancement to Regex provided by "jeffreypriebe" because he needed a kind of YouTube URL is the URL of the videos when they are looking through a channel.

Well no but this is the function that I have armed.

<script type="text/javascript"> function youtube_parser(url){     var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/;     var match = url.match(regExp);     return (match&&match[7].length==11)? match[7] : false; } </script> 

These are the types of URLs supported

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&amp;hl=en_US&amp;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 

Can be found in [http://web.archive.org/web/20160926134334/] http://lasnv.net/foro/839/Javascript_parsear_URL_de_YouTube

vote vote

88

I simplified Lasnv's answer a bit.

It also fixes the bug that WebDeb describes.

Here it is:

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 } 

Here is a regexer link to play with: http://regexr.com/3dnqv

vote vote

75

You don't need to use a regular expression for this.

var video_id = window.location.search.split('v=')[1]; var ampersandPosition = video_id.indexOf('&'); if(ampersandPosition != -1) {   video_id = video_id.substring(0, ampersandPosition); } 
vote vote

63

None of these worked on the kitchen sink as of 1/1/2015, notably URLs without protocal http/s and with youtube-nocookie domain. So here's a modified version that works on all these various Youtube versions:

// Just the regex. Output is in [1]. /^.*(?:(?:youtu\.be\/|v\/|vi\/|u\/\w\/|embed\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/  // For testing. var urls = [     '//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\/)|(?:(?:watch)?\?v(?:i)?=|\&v(?:i)?=))([^#\&\?]*).*/;  for (i = 0; i < urls.length; ++i) {     r = urls[i].match(rx);     console.log(r[1]); } 
vote vote

56

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

Tested on:

  • 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

Inspired by this other answer.

Top 3 video Explaining javascript - How do I get the YouTube video ID from a URL?

Related QUESTION?