Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
@schurpf

This comment has been minimized.

Copy link

schurpf commented May 23, 2014

Thanks! Just one comment for other people coming across this, you may need to escape the "' symbols:
/(youtu.be/|youtube.com/(watch?(.*&)?v=|(embed|v)/))([^?&"'>]+)/

@Protuhj

This comment has been minimized.

Copy link

Protuhj commented Jun 27, 2014

This seems to be a better regex (ig flags) - It is much more flexible to the input text:

Match is in 1st match group, all other groups are non-capturing:
(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})\W  

Tested against this text at regex101.com:

 var url = "http://www.youtube.com/user/Scobleizer#p/u/1/1p3vONEsYGo
url = "http://youtu.be/NLqAFTWOVbY
var url = "http://www.youtube.com/embed/NLqTHREEVbY https://www.youtube.com/embed/NLqAFOURVbY
var url = "http://www.youtube.com/v/NLqAFFIVEbY?fs=1&hl=en_US
var url = "http://www.youtube.com/watch?v=NLqASIXrVbY
var url = "http://www.youtube.com/user/Scobleizer#p/u/1/1pSEVENsYGo
var url = "http://www.youtube.com/ytscreeningroom?v=NRHEIGHTx8I
var url = "http://www.youtube.com/user/Scobleizer#p/u/1/1p3NINEsYGo
var url = "http://www.youtube.com/watch?v=JYATEN_TzhA&feature=featured
# Parses YouTube URLs directly or from iframe code. Handles:
# * Address bar on YouTube url (ex: http://www.youtube.com/watch?v=ZFELEVEN-OI)
# * Direct http://youtu.be/ url (ex: http://youtu.be/ZFTWELVEBOI)
# * Full iframe embed code (ex: <iframe src="http://www.youtube.com/embed/13_lHhCNBOI">)
# * Old <object> tag embed code (ex: <object><param name="movie" value="http://www.youtube.com/v/FOURTEEN_--">...)
http://www.youtube.com/user/Scobleizer#p/u/1/1p3vLASTYGo
@toots

This comment has been minimized.

Copy link

toots commented Aug 14, 2014

This last regexp does not work with ruby:

> url = "http://www.youtube.com/watch?v=NLqASIXrVbY"
=> "http://www.youtube.com/watch?v=NLqASIXrVbY"
> vid_regex = /(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})\W/
=> /(?:youtube(?:-nocookie)?\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})\W/
> url =~ vid_regex
=> nil
> $1
=> nil
@dgilperez

This comment has been minimized.

Copy link

dgilperez commented Aug 26, 2014

@toots it works for me removing the last \W

@rolfen

This comment has been minimized.

Copy link

rolfen commented Feb 16, 2018

Same thing for php - must remove trailing \W. I also had to enclose it in slashes /.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.