I want to return the video id from the video url of YouTube even for Shorts.
But I have got pattern which works for some few url which do not includes Shorts
^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*
Edit: It should work for the following urls:
;feature=feedrec_grec_index
;amp;hl=en_US&rel=0
#t=0m10s
Thank You
I want to return the video id from the video url of YouTube even for Shorts.
But I have got pattern which works for some few url which do not includes Shorts
^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*
Edit: It should work for the following urls:
http://www.youtube./watch?v=0zM3nApSvMg&feature=feedrec_grec_index
http://www.youtube./user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
http://www.youtube./v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
http://www.youtube./watch?v=0zM3nApSvMg#t=0m10s
http://www.youtube./embed/0zM3nApSvMg?rel=0
http://www.youtube./watch?v=0zM3nApSvMg
http://youtu.be/0zM3nApSvMg
https://youtube./shorts/0dPkkQeRwTI?feature=share
https://youtube./shorts/0dPkkQeRwTI
Thank You
Share Improve this question edited Feb 6, 2022 at 3:56 Amrit Bera asked Feb 5, 2022 at 16:55 Amrit BeraAmrit Bera 1662 silver badges10 bronze badges 2- Can you provide both a valid video url and shorts url so we can provide a solution for you? – Simon K Commented Feb 5, 2022 at 16:57
- @Simon_K i have provided the url – Amrit Bera Commented Feb 6, 2022 at 3:57
2 Answers
Reset to default 13This should work on the provided examples
(youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])|$))
https://regex101./r/5JhmpW/1 The actual video id should be the third capture group in each match.
- Group1: Url up to the last part
- Group2: The last part of the url which receives the videoId as parameter
- Group3: Either the last part in the url (which is the videoId in the "/v/" , "/embed/", "/user/", "/shorts/" and "youtu.be/" variants ), or the videoId parameter (in the case of watch)
You can use it in javascript like this:
let data = `http://www.youtube./watch?v=0zM3nApSvMg&feature=feedrec_grec_index
http://www.youtube./user/IngridMichaelsonVEVO#p/a/u/1/QdK8U-VIH_o
http://www.youtube./v/0zM3nApSvMg?fs=1&hl=en_US&rel=0
http://www.youtube./watch?v=0zM3nApSvMg#t=0m10s
http://www.youtube./embed/0zM3nApSvMg?rel=0
http://www.youtube./watch?v=0zM3nApSvMg
http://youtu.be/0zM3nApSvMg
https://youtube./shorts/0dPkkQeRwTI?feature=share
https://youtube./shorts/0dPkkQeRwTI`;
let regex = /(youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])|$))/gm;
let videoIds = [...data.matchAll(regex)].map(x => x[3]);
Or if you only expect one url at a time:
function getVideoId(url) {
let regex = /(youtu.*be.*)\/(watch\?v=|embed\/|v|shorts|)(.*?((?=[&#?])|$))/gm;
return regex.exec(url)[3];
}
keep in mind regex in javascript aren't stateless, running the same regex multiple times will cause it to iterate through the matches in the text (and eventually return NULL once it has reached the end), which is why the regex is re initialized on every call in this case. If no matches are found, it will also return null.
Regex for 1 expected URL at a time that captures id group that can be accessed via named groups MDN.
^(?:(?:https|http):\/\/)?(?:www\.)?(?:youtube\.|youtu\.be).*(?<=\/|v\/|u\/|embed\/|shorts\/|watch\?v=)(?<!\/user\/)(?<id>[\w\-]{11})(?=\?|&|$)
As I need to return ID or false, this is how I wrote the function:
function youtubeLinkToEmbed(link) {
const youtubeIdRegex =
/^(?:(?:https|http):\/\/)?(?:www\.)?(?:youtube\.|youtu\.be).*(?<=\/|v\/|u\/|embed\/|shorts\/|watch\?v=)(?<!\/user\/)(?<id>[\w\-]{11})(?=\?|&|$)/;
return link.match(youtubeIdRegex)?.groups?.id || false;
}
UPD: As of 10.2022, Safari/Webkit provide no support for lookbehind syntax thus using above might break your web apps.
I'll leave below logic and uglified regex I made for backwards patibility with Webkit.
function stripYoutubeId(link) {
if (!link) {
return false;
}
const youtubeIdRegex =
/^(?:(?:https|http):\/\/)?(?:www\.)?(?:youtube\.|youtu\.be).*?(?:\/|v\/|u\/|embed\/|shorts\/|watch\?v=|(?<username>user\/))(?<id>[\w\-]{11})(?:\?|&|$)/;
const match = link.match(youtubeIdRegex);
// checks if 'user/' is located right before expected id, in which case it would return username instead
if (match?.groups?.username || !match?.groups?.id) {
return false;
}
return `https://www.youtube./embed/${match.groups.id}`;
}