Skip to content

Instantly share code, notes, and snippets.

@nuc
Forked from afeld/gist:1254889
Created October 19, 2016 11:02
Show Gist options
  • Save nuc/fc28090296e0d95108ab19b46f870935 to your computer and use it in GitHub Desktop.
Save nuc/fc28090296e0d95108ab19b46f870935 to your computer and use it in GitHub Desktop.
YouTube video ID regex
# Parses YouTube URLs directly or from iframe code. Handles:
# * Address bar on YouTube url (ex: http://www.youtube.com/watch?v=ZFqlHhCNBOI)
# * Direct http://youtu.be/ url (ex: http://youtu.be/ZFqlHhCNBOI)
# * Full iframe embed code (ex: <iframe src="http://www.youtube.com/embed/ZFqlHhCNBOI">)
# * Old <object> tag embed code (ex: <object><param name="movie" value="http://www.youtube.com/v/ZFqlHhCNBOI">...)
/(youtu\.be\/|youtube\.com\/(watch\?(.*&)?v=|(embed|v)\/))([^\?&"'>]+)/
$5 #=> the video ID
# test it on Rubular: http://rubular.com/r/eaJeSMkJvo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment