From all these different URL formats, I want to extract:
dQw4w9WgXcQ
My Attempt
I tried using regular expressions but I’m having trouble handling all the different YouTube URL variations. Some URLs have additional parameters, some use the shortened youtu.be format, and others have different query string structures.
function extractVideoId(url) {
// My current approach isn't working for all cases
let match = url.match(/v=([^&]+)/);
return match ? match[1] : null;
}
This only works for some formats but fails with shortened URLs and other variations. What’s the most reliable way to handle all YouTube URL types?
honestly the URL constructor method is probaly your best bet here. just wrap it in a try-catch since some malformed urls can throw errors. something like new URL(link).searchParams.get('v') for regular youtube links and check if hostname includes ‘youtu.be’ then grab the pathname
try this quick solution - use URLSearchParams for the query string and handle youtu.be separately. something like let url = new URL(yourUrl); return url.searchParams.get('v') || url.pathname.slice(1) should work for most cases. might need tweaking but its a good starting point
Been working with YouTube APIs for years and encountered this same headache multiple times. What I found works reliably is a hybrid approach that first normalizes the URL structure then extracts the ID. The main issue with most solutions is they don’t account for all the edge cases YouTube throws at you - like URLs with timestamps, playlist parameters, or the various mobile formats. My approach starts by creating a URL object to handle malformed inputs gracefully, then I check the hostname to determine the extraction method. For youtube.com domains, I prioritize the ‘v’ parameter but also check for embed paths. For youtu.be, the ID is always the first path segment. The crucial part is validating that what you extract is actually 11 characters and contains only valid YouTube ID characters. I also strip out any trailing parameters that sometimes get attached to the ID itself. This method has handled everything from old-style URLs to the newest mobile sharing formats without breaking.
A comprehensive regex pattern handles this better than URL parsing in my experience. I use this approach that covers all YouTube variations:
function extractVideoId(url) {
const pattern = /(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/;
const match = url.match(pattern);
return match ? match[1] : null;
}
This pattern catches standard watch URLs, shortened youtu.be links, embed URLs, and mobile versions. The key insight is that YouTube video IDs are always exactly 11 characters long, so the regex enforces that constraint. I’ve tested this with hundreds of URLs in production and it rarely fails. Much more reliable than splitting on URL components since YouTube keeps changing their URL structure.
I’ve dealt with this exact problem before and found that combining URL parsing with regex works better than either approach alone. The issue with pure regex is that YouTube has too many edge cases, but pure URL parsing misses some formats too. What worked for me was checking the hostname first, then extracting accordingly. For youtube.com URLs, check both the ‘v’ parameter and the pathname (for embed URLs like /embed/videoId). For youtu.be, the video ID is always in the pathname after the first slash. Also watch out for playlist URLs and live streams - they have different patterns. One gotcha I ran into was that some URLs have timestamps or other fragments that need to be stripped from the video ID. Make sure to clean the extracted ID by removing anything after ‘&’ or ‘?’ characters that might sneak through.