Skip to content

Instantly share code, notes, and snippets.

@shabith
Last active November 4, 2021 12:51
Show Gist options
  • Save shabith/9801530 to your computer and use it in GitHub Desktop.
Save shabith/9801530 to your computer and use it in GitHub Desktop.
Regex to extract video ID from Vimeo or Youtube URL
//check for video URL
/* http://lasnv.net/foro/839/Javascript_parsear_URL_de_YouTube */
var video_id_regExp = /^.*((youtu.be\/|vimeo.com\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/,
match = url.match(video_id_regExp),
video_id;
if (match&&match[7]){
//valid
video_id = match[7];
}else{
//invalid
alert("Invalid Video URL");
}
/**
* These are the types of URLs supported
*
* youtube
* =======
*
* http://www.youtube.com/watch?v=0zM3nApSvMg&feature=feedrec_grec_index
* http://www.youtube.com/user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
* http://www.youtube.com/v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
* http://www.youtube.com/watch?v=0zM3nApSvMg#t=0m10s
* http://www.youtube.com/embed/0zM3nApSvMg?rel=0
* http://www.youtube.com/watch?v=0zM3nApSvMg
* http://youtu.be/0zM3nApSvMg
*
* vimeo
* =====
*
* https://vimeo.com/67736784
* */
@kdrich
Copy link

kdrich commented Dec 20, 2017

This wouldn't handle https://vimeo.com/channels/staffpicks/242573626

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment