Extract youtube video ID from url or share link

function youtubeVideoId(url){ var matches1 = url.match(/v=([\w-_]+)/), // Matches the v= part of the youtube url matches2 = url.match(/youtu.be\/([\w-_]+)/); // Matches a youtube.be url if(matches1){ return matches1[1]; } else if(matches2){ return matches2[1] } else{ return url; } }
Given a URL such as: ' http://www.youtube.com/watch?v=l40pzaWhC2M' or ' http://youtu.be/l40pzaWhC2M'

This function will return just the ID of the video, in this case 'l40pzaWhC2M'

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.