How to extract Video Id from youtube URL

Home » Javascript Snippets » How to extract Video Id from youtube URL
Javascript Snippets No Comments
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <meta charset="utf-8">

<meta name="robots" content="noindex">
  <title>JS Bin</title>
</head>
<body>
  Video url
  <input type="text" id="videourl" />
  <input type="button" id="button" value="check" />
  
<script>
$("#button").click(function(){
  
  
    var videolink =$("#videourl").val();

var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = videolink.match(regExp);


  if (match && match[7].length == 11) 
    {
      alert("youtube video id : "+ match[7]);      
      
      return;
    }
    regExp = "vimeo\\.com/(?:.*#|.*/videos/)?([0-9]+)";
     match = videolink.match(regExp);
    if(match)
      {
       var videoid = videolink.split('/')[videolink.split('/').length - 1];
        
        alert("vimeo video id :"+videoid);
      }

else 
  {
    alert("Unknown url");
  }

  
  
  
});
</script>
<script src="http://static.jsbin.com/js/render/edit.js?3.25.14"></script>
<script>jsbinShowEdit && jsbinShowEdit({"static":"http://static.jsbin.com","root":"http://jsbin.com"});</script>

<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-1656750-34', 'jsbin.com');
ga('require', 'linkid', 'linkid.js');
ga('require', 'displayfeatures');
ga('send', 'pageview');

</script>

</body>
</html>
loading...

LEAVE A COMMENT