0

I'm looking for a way to use Regular Expression to scan through the following scenarios and return the youtube video ID of the specific video.

youtu.be/dQw4w9WgXcQ
www.youtube.com/watch?v=dQw4w9WgXcQ
https://www.youtube.com/watch?v=dQw4w9WgXcQ
https://www.youtube.com/watch?v=dQw4w9WgXcQ&index=2&list=PLsBI83Bzbnacim19bTUONClIGpT22Zfkw

All of the following cases should capture just dQw4w9WgXcQ (the raw video ID)

Any idea of what regex I'd use to capture this? Keep in mind that youtube links can also include -'s and _'s, so \w+ will not always capture the video ID properly.

Thanks!

Izzy
  • 272
  • 1
  • 14
  • So `/https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube(?:-nocookie)?\.com\S*[^\w\s-])([\w-]{11})(?=[^\w-]|$)(?![?=&+%\w.-]*(?:['"][^<>]*>|<\/a>))[?=&+%\w.-]*/ig` would give me what I want...? Is there any way to... shorten this? – Izzy Jul 29 '15 at 09:56

1 Answers1

0

I figured it out on my own. I believe this is a duplicate of "How to find all Youtube video ids in a string using a regex?", however it provided a very lengthy regex to accept many cases that I wouldn't encounter.

The following regex worked and captured only what I wanted: /(?:\.be\/|watch\?v=)([^\s&]+)/

Sorry and thanks!

Community
  • 1
  • 1
Izzy
  • 272
  • 1
  • 14