[JS] Youtube Video ID URL Parser

Extract video id from youtube URL with javascript function
function youtube_parser(url){
    var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
    var match = url.match(regExp);
    if (match&&match[7].length==11) { return match[7]}
  
  var regExp2 = /^.*(youtu\.be\/|v\/|u\/\w\/|embed\/|watch\?v=|\&v=)([^#\&\?]*).*/;
  var match2 = url.match(regExp2);
  if (match2 && match2[2].length == 11) {
    return match2[2];
  }
  
  var regex = /(?:[?&]v=|\/embed\/|\/1\/|\/v\/|https?:\/\/(?:www\.)?youtu\.be\/)([^&\n?#]+)/gm;
  let m = regex.exec(url);
  if (m[1].length){
    return m[1];
  }
  
  return url+' --failed--';
}
console.clear()
var n = [
  'http://www.youtube.com/watch?v=0zMd3nApSvMg&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=0zMg3nApSvMg#t=0m10s',
  'http://www.youtube.com/embed/0zM3nApdSvMg?rel=0',
  'http://www.youtube.com/watch?v=0zM3nAxpSvMg',
  'http://youtu.be/0zM3nApgSvMg'
];
  n.forEach(function(i){
  document.write(youtube_parser(i)+'<br>')
});
demo

See the Pen Youtube URL Parser by dimas lanjaka (@dimaslanjaka) on CodePen.

Subscribe to receive free email updates:

0 Response to "[JS] Youtube Video ID URL Parser"

Post a Comment

Bila Ada posting yang kurang, atau error atau yang lainnya, silahkan tinggalkan komentar agar artikel/post di perbaiki.
Jangan Lupa Cek Box "Notify Me" agar tahu komentar kamu dibalas oleh saya.

If there are any posts that are missing, or error or anything else, please leave a comment for the article / post to be fixed.
Do not Forget Check Box "Notify Me" to know our comments replied by me.