Last Updated: February 25, 2016
·
1.211K
· retgoat

Regex to cut video id from youtube url

Sometimes we need to extract video ID from youtube url.
Here is my approch:

url = "//www.youtube-nocookie.com/embed/8sfzigidWqI?list=UUCZKJ_p08CuihXJQUF04Ljg\"

video_id = url.match(/(src=|src=\\)\"(\/\/|http:\/\/)?(www\.)(youtube.com|youtube-nocookie.com|youtu.be)\/embed\/(.{11})(.+)\"/)[5]
puts video_id
#=> "8sfzigidWqI"