Skip to content

Instantly share code, notes, and snippets.

@peatiscoding
Created August 23, 2018 11:51
Show Gist options
  • Save peatiscoding/d31a5690413c6f8e99c4c5d0772f85d2 to your computer and use it in GitHub Desktop.
Save peatiscoding/d31a5690413c6f8e99c4c5d0772f85d2 to your computer and use it in GitHub Desktop.
(JS) Regular Expression to match youtube video links
const rgx = /(?:https?:)?\/{2}(?:www\.)?youtu\.?be(?:\/|\.com\/watch\?v\=|\.com\/v\/|\.com\/embed\/)?([\w-]*)[?&]?.*/
// Support:
// - Simple = https://www.youtube.com/watch?v=I2aC297uaZ4&t=120
// - Short = https://youtube.com/embed/_fIABbpAsjQ?t=120
// - Shorter = https://youtu.be/I2aC297uaZ4?t=9
// - Even Shorter = //youtu.be/I2aC297uaZ4?t=9
// TEST:
[
'https://www.youtube.com/watch?v=I2aC297uaZ4&t=120',
'https://youtube.com/embed/_fIABbpAsjQ?t=120',
'https://youtu.be/I2aC297uaZ4?t=9',
'//youtu.be/I2aC297uaZ4?t=9'
].map((o) => o.match(rgx)[1])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment