Skip to content

Instantly share code, notes, and snippets.

@Radagaisus
Created November 29, 2012 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Radagaisus/4170686 to your computer and use it in GitHub Desktop.
Save Radagaisus/4170686 to your computer and use it in GitHub Desktop.
Get a YouTube Video ID from a Url
# 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
@Radagaisus
Copy link
Author

This is a prime example of tabs and spaces getting together with one goal: making me rage.

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