Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Get YouTube ID from various YouTube URL using JavaScript
/**
* Get YouTube ID from various YouTube URL
* @author: takien
* @url: http://takien.com
* For PHP YouTube parser, go here http://takien.com/864
*/
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;
}
/*
* Tested URLs:
var url = 'http://youtube.googleapis.com/v/4e_kz79tjb8?version=3';
url = 'https://www.youtube.com/watch?feature=g-vrec&v=Y1xs_xPb46M';
url = 'http://www.youtube.com/watch?feature=player_embedded&v=Ab25nviakcw#';
url = 'http://youtu.be/Ab25nviakcw';
url = 'http://www.youtube.com/watch?v=Ab25nviakcw';
url = '<iframe width="420" height="315" src="http://www.youtube.com/embed/Ab25nviakcw" frameborder="0" allowfullscreen></iframe>';
url = '<object width="420" height="315"><param name="movie" value="http://www.youtube-nocookie.com/v/Ab25nviakcw?version=3&amp;hl=en_US"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/Ab25nviakcw?version=3&amp;hl=en_US" type="application/x-shockwave-flash" width="420" height="315" allowscriptaccess="always" allowfullscreen="true"></embed></object>';
url = 'http://i1.ytimg.com/vi/Ab25nviakcw/default.jpg';
url = 'https://www.youtube.com/watch?v=BGL22PTIOAM&feature=g-all-xit';
url = 'BGL22PTIOAM';
*/
@omahboub

This comment has been minimized.

Copy link

commented Oct 19, 2015

Work like a charm!! thank you so much man.

@miglrodri

This comment has been minimized.

Copy link

commented Nov 28, 2015

Great mate! Thanks.

@DixonGarcia

This comment has been minimized.

Copy link

commented Mar 13, 2016

Thanks buddy!

@joshstauffer

This comment has been minimized.

Copy link

commented Apr 5, 2016

Thanks for this! May need to change the else...

ID = url.toString();

@eduardinni

This comment has been minimized.

@dskvr

This comment has been minimized.

Copy link

commented Jun 29, 2016

👍

@LKWLaLa

This comment has been minimized.

Copy link

commented Jul 1, 2016

This is great - thank you!

@csepulveda-xumak

This comment has been minimized.

Copy link

commented Feb 22, 2017

@takien This is a short version of your Gist

function YouTubeGetID(url){
  url = url.split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
  return (url[2] !== undefined) ? url[2].split(/[^0-9a-z_\-]/i)[0] : url[0];
}
@jtewright

This comment has been minimized.

Copy link

commented Apr 15, 2017

👍

@ronitonit

This comment has been minimized.

Copy link

commented May 22, 2017

@RyanCasas

This comment has been minimized.

Copy link

commented Aug 24, 2017

I added @ronitonit URLs to @csepulveda-xumak 's version:

function YouTubeGetID(url){
    url = url.split(/(vi\/|v%3D|v=|\/v\/|youtu\.be\/|\/embed\/)/);
    return undefined !== url[2]?url[2].split(/[^0-9a-z_\-]/i)[0]:url[0];
}
@andrewarosario

This comment has been minimized.

Copy link

commented Jan 17, 2018

Great! Thank you so much!

@Joshfindit

This comment has been minimized.

Copy link

commented Jan 29, 2018

Please test the following:

https://www.youtube.com/red
https://www.youtube.com/channel/UCEgdi0XIXXZ-qJOFPf4JSKw

Currently, they match successfully and return a bad Video ID

@mvww11

This comment has been minimized.

Copy link

commented Mar 31, 2018

Thanks!!

YouTubeGetID('http://www.youtube.com/watch?feature=player_embedded&v=Ab25nviakcw123123#');
returns a string.

YouTubeGetID('Ab25nviakcw123123');
returns an array.

To make the last one returns a string too, just replate ID = url; for ID = url[0]; in the else clause

@Soullighter

This comment has been minimized.

Copy link

commented Jun 19, 2018

For PHP YouTube parser, go here http://takien.com/864

The URL does not exist, can you provide a new one?

@banada

This comment has been minimized.

Copy link

commented Jul 13, 2018

This is great! Can I use this in my project? There is no license right now.

@JetmirAhmati

This comment has been minimized.

Copy link

commented Aug 29, 2018

hi all,
I tested here with these url and seems that is not working
https://www.youtube-nocookie.com/embed//5ybGYHlgNGo
Could somebody help ?

@Vuice

This comment has been minimized.

Copy link

commented Nov 29, 2018

hi all,
I tested here with these url and seems that is not working
https://www.youtube-nocookie.com/embed//5ybGYHlgNGo
Could somebody help ?

Just in case you haven't figured it out yet, you have 2 "/" after "embed", making the string invalid.

@kangfarih

This comment has been minimized.

Copy link

commented Feb 26, 2019

Thank you so much,..

@EylonSu

This comment has been minimized.

Copy link

commented May 30, 2019

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.