0

I am using following function to parse YouTube URL to retrieve a YouTube ID from a YouTube URL:

function yt_parser()
{
    var yt_url_sub = http://www.youtube.com/watch?v=6nZlXB5okeo; 
    var youtube_id = yt_url_sub.replace(/^[^v]+v.(.{11}).*/,"$1");
    alert(youtube_id);  
}

output : 6nZlXB5okeo

but how should I validate whether this URL is from YouTube or not?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Rushikesh jogle
  • 591
  • 2
  • 9
  • 29
  • Well, the host being `youtube.com` is a strong indication... however, Youtube also uses `youtu.be`. You'll never be able to tell with 100% certainty I guess. – Pekka Jan 15 '13 at 12:02
  • 1
    Possible duplicate of http://stackoverflow.com/questions/2936467/parse-youtube-video-id-using-preg-match – Salman A Jan 15 '13 at 12:07
  • I think this post will do exactly what you're wanting with any valid youtube URL: http://stackoverflow.com/a/10315969/485418 – Samsquanch Jan 15 '13 at 15:59

1 Answers1

1

You can change your regex for this

var url = 'http://www.youtube.com/watch?v=wBnCURIfbPg'

var m = url.match(/:\/\/www.youtube.com\/.*?\bv=([^&]+)/);

if (m) {
  alert(m[1]);
}

http://jsfiddle.net/zmDZp/

Francis
  • 3,335
  • 20
  • 46