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 omahboub commented Oct 19, 2015

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

@miglrodri

This comment has been minimized.

Copy link

@miglrodri miglrodri commented Nov 28, 2015

Great mate! Thanks.

@DixonGarcia

This comment has been minimized.

Copy link

@DixonGarcia DixonGarcia commented Mar 13, 2016

Thanks buddy!

@joshstauffer

This comment has been minimized.

Copy link

@joshstauffer 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 dskvr commented Jun 29, 2016

👍

@LKWLaLa

This comment has been minimized.

Copy link

@LKWLaLa LKWLaLa commented Jul 1, 2016

This is great - thank you!

@csepulveda-xumak

This comment has been minimized.

Copy link

@csepulveda-xumak 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 jtewright commented Apr 15, 2017

👍

@ronitonit

This comment has been minimized.

Copy link

@ronitonit ronitonit commented May 22, 2017

@RyanCasas

This comment has been minimized.

Copy link

@RyanCasas 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 andrewarosario commented Jan 17, 2018

Great! Thank you so much!

@Joshfindit

This comment has been minimized.

Copy link

@Joshfindit 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 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 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 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 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 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 kangfarih commented Feb 26, 2019

Thank you so much,..

@EylonSu

This comment has been minimized.

Copy link

@EylonSu EylonSu commented May 30, 2019

Thanks!

@kamenos96

This comment has been minimized.

Copy link

@kamenos96 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 JoshuaFrontEnd commented Dec 18, 2019

Thanks!! 👏

@avag01

This comment has been minimized.

Copy link

@avag01 avag01 commented Dec 20, 2019

Thanks!

@wenogk

This comment has been minimized.

Copy link

@wenogk wenogk commented Dec 28, 2019

Amazing.

@katai5plate

This comment has been minimized.

Copy link

@katai5plate 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 btroncone commented Mar 28, 2020

Thanks for the TypeScript version, works perfectly!

@raziEiL

This comment has been minimized.

Copy link

@raziEiL raziEiL commented Aug 9, 2020

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];
}

Optimized typescript version (immutable, camelCase style)

function getYouTubeId(url: string) {
    const arr = url.split(/(vi\/|v%3D|v=|\/v\/|youtu\.be\/|\/embed\/)/);
    return undefined !== arr[2] ? arr[2].split(/[^\w-]/i)[0] : arr[0];
}
@willowell

This comment has been minimized.

Copy link

@willowell willowell commented Sep 25, 2020

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];
}

Optimized typescript version (immutable, camelCase style)

function getYouTubeId(url: string) {
    const arr = url.split(/(vi\/|v%3D|v=|\/v\/|youtu\.be\/|\/embed\/)/);
    return undefined !== arr[2] ? arr[2].split(/[^\w-]/i)[0] : arr[0];
}

Hello! May I use this function in a website I'm working on?

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.