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

omahboub commented Oct 19, 2015

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

@miglrodri

This comment has been minimized.

Copy link

miglrodri commented Nov 28, 2015

Great mate! Thanks.

@DixonGarcia

This comment has been minimized.

Copy link

DixonGarcia commented Mar 13, 2016

Thanks buddy!

@joshstauffer

This comment has been minimized.

Copy link

joshstauffer 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

dskvr commented Jun 29, 2016

👍

@LKWLaLa

This comment has been minimized.

Copy link

LKWLaLa commented Jul 1, 2016

This is great - thank you!

@csepulveda-xumak

This comment has been minimized.

Copy link

csepulveda-xumak 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

jtewright commented Apr 15, 2017

👍

@ronitonit

This comment has been minimized.

Copy link

ronitonit commented May 22, 2017

@RyanCasas

This comment has been minimized.

Copy link

RyanCasas 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

andrewarosario commented Jan 17, 2018

Great! Thank you so much!

@Joshfindit

This comment has been minimized.

Copy link

Joshfindit 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

mvww11 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

Soullighter 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

banada 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

JetmirAhmati 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

Vuice 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

kangfarih commented Feb 26, 2019

Thank you so much,..

@EylonSu

This comment has been minimized.

Copy link

EylonSu commented May 30, 2019

Thanks!

@kamenos96

This comment has been minimized.

Copy link

kamenos96 commented Dec 5, 2019

A quick fix for that kind of links https://www.youtube-nocookie.com/embed//5ybGYHlgNGo /(vi\/|v%3D|v=|\/v\/|youtu\.be\/|\/embed\/\/?)/

@JoshuaFrontEnd

This comment has been minimized.

Copy link

JoshuaFrontEnd commented Dec 18, 2019

Thanks!! 👏

@avag01

This comment has been minimized.

Copy link

avag01 commented Dec 20, 2019

Thanks!

@wenogk

This comment has been minimized.

Copy link

wenogk commented Dec 28, 2019

Amazing.

@katai5plate

This comment has been minimized.

Copy link

katai5plate commented Jan 6, 2020

Great!

TypeScript:

const youTubeGetID = (url: string) => {
  const [a, , b] = url
    .replace(/(>|<)/gi, '')
    .split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed\/)/);
  if (b !== undefined) {
    return b.split(/[^0-9a-z_-]/i)[0];
  } else {
    return a;
  }
};
@btroncone

This comment has been minimized.

Copy link

btroncone commented Mar 28, 2020

Thanks for the TypeScript version, works perfectly!

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.