Skip to content

Instantly share code, notes, and snippets.

@bdeleasa
Created June 12, 2020 19:00
Show Gist options
  • Save bdeleasa/176167d4995d03dcd8ea970124d946e0 to your computer and use it in GitHub Desktop.
Save bdeleasa/176167d4995d03dcd8ea970124d946e0 to your computer and use it in GitHub Desktop.
Javascript function that returns the YouTube ID when given a YouTube video URL.
/**
* Function that returns the YouTube ID when given the YouTube URL.
*
* @param url string
* @return int|null
*/
function get_youtube_id_from_url(url) {
const regExp = /^.*(youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=|&v=)([^#&?]*).*/;
const match = url.match(regExp);
return (match && match[2].length === 11)
? match[2]
: null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment