[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.

Comments

Popular posts from this blog

Install deb package via Termux

How to Make Blogger Safelink Converter v3

List of weapon buff materials, effects and how it can be durango wild lands