Created
August 23, 2018 11:51
-
-
Save peatiscoding/d31a5690413c6f8e99c4c5d0772f85d2 to your computer and use it in GitHub Desktop.
(JS) Regular Expression to match youtube video links
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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