Get YouTube video id form URL using Javascript.

Mahabubur Rahman
3

In this topics I will discus about "How to get YouTube video id from its URL". For this create a simple javascript function as bellow -
function YouTubeGetID(url){
var ID = '';
url = url.replace(/(>|<)/gi,'').split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
if(url[2] !== undefined) {
ID = url[2].split(/[^0-9a-z_\-]/i);
ID = ID[0];
}
else {
ID = url;
}
return ID;
}
Call the function as bellow -
var youtubeurl="https://www.youtube.com/watch?v=_Yt9Xfo4VN4";
YouTubeGetID(youtubeurl);
And then you will get something output for example url as bellow -
_Yt9Xfo4VN4
I hope it will helpful for you.
Now it's time for enjoy.

For any comment or suggestion on this topics, please mention your comment on comment box bellow.  

Post a Comment

3Comments
Post a Comment