Created
June 12, 2020 19:00
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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