Created
November 29, 2012 17:43
-
-
Save Radagaisus/4170686 to your computer and use it in GitHub Desktop.
Get a YouTube Video ID from a 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
# Parses a URL of a YouTube video and | |
# returns the video id. | |
# | |
# - url - the url of the video | |
# | |
youtube_url_parser = (url) -> | |
regex = ///^ | |
.* # Zero or more of any character | |
( # Group 1, either: | |
(youtu.be/) # youtu.be/ - Group 2 | |
| (v/) # v/ - Group 3 | |
| (/u/\w/) # /u/[a-zA-Z0-9_]/ - Group 4 | |
| (embed/) # embed/ - Group 5 | |
| (watch\?) # watch? - Group 6 | |
) | |
\?? # optional question mark | |
v?=? # optional v= | |
( | |
[^#\&\?]* # Group 7 - The Video ID. | |
# anything but #,&,? zero | |
# or more times. | |
) | |
.* # Any character. | |
/// | |
match = url.match regex | |
if match && match[7].length is 11 | |
then return match[7] | |
else return null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a prime example of tabs and spaces getting together with one goal: making me rage.