Free Photos
WordPress Themes

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(/\/([\w-_]+)/); // Matches a url if(matches1){ return matches1[1]; } else if(matches2){ return matches2[1] } else{ return url; } }
Given a URL such as: '' or ''

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.