views:

711

answers:

2

I know there is plenty of question answered over here http://stackoverflow.com/questions/tagged/youtube+regex, but not able find a question similar to me.

Any body has the JavaScript Regular expression for validating the YouTube VIDEO URL's line below listed. Just want to know where such a URL can be possible

http://www.youtube.com/watch?v=bQVoAWSP7k4
http://www.youtube.com/watch?v=bQVoAWSP7k4&feature=popular
http://www.youtube.com/watch?v=McNqjYiFmyQ&feature=related&bhablah
http://youtube.com/watch?v=bQVoAWSP7k4

-- update 1-- -- update 2--

This one worked almost fine, but failed for the URL http://youtube.com/watch?v=bQVoAWSP7k4

var matches = $('#videoUrl').val().match(/http:\/\/(?:www\.)?youtube.*watch\?v=([a-zA-Z0-9\-_]+)/);
if (matches) {
    alert('valid');
} else {
    alert('Invalid');
}
+2  A: 
^http:\/\/(?:www\.)?youtube.com\/watch\?v=\w+(&\S*)?$

//if v can be anywhere in the query list

^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=\w+)(?:\S+)?$
Amarghosh
Mithun P
@Mithun check the update
Amarghosh
+1  A: 

You can not match the id-part with \w+, as it does not include the dash character (-). [a-zA-Z0-9_-]+ would be something more correct.

finivisio