You work with youtube URL? You can use this usefull regex to get the video id from the given youtube url.
function getYoutubeVideoId(url) {
const regex =
/(?:\/embed\/|\/v\/|\/watch\?v=|youtu\.be\/|\/shorts\/)([a-zA-Z0-9_-]+)/;
const match = url.match(regex);
return match ? match[1] : null;
}
// Typical youtube urls:
const youtubeUrls = [
"https://www.youtube.com/watch?v=VIDEO_ID",
"https://youtu.be/VIDEO_ID",
"https://www.youtube.com/embed/VIDEO_ID",
"https://www.youtube.com/v/VIDEO_ID?version=3&autohide=1",
"https://www.youtube.com/shorts/VIDEO_ID",
];
youtubeUrls.forEach((url) => {
const videoId = getYoutubeVideoId(url);
if (videoId) {
console.log("YouTube Video ID: " + videoId);
} else {
console.log("Invalid YouTube URL");
}
});