views:

29

answers:

1

Hi all, this is how i normally check for youtube url validity using javascript. It works great but fails for urls with "-" before the video id eg http://www.youtube.com/watch?v=-pIaQpwYEjY

Any remedy available as I'm not well versed with regex

var matches = $('#as_url').val().match(/^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=\w+)(?:\S+)?$/);
if (matches) {
} else {
    error +="\nInvalid Youtube Url";
}
+4  A: 

Change \w+ to [\w-]+, since the \w character class only matches [A-Za-z0-9_].

Regular-Expressions.info has a good explanation of what character classes are, as well as the lookahead feature of regexes, both of which your regex uses.

idealmachine
Just made the ammendment and tested it with same url but it alerts invalid youtube url
Sir Lojik
+1! @Sir Lojik: This works! Just tested it myself. There is probably a typo in your regular expression.
elusive
this fails for me var matches = $('#as_url').val().match(/^http:\/\/(?:www\.)?youtube.com\/watch\?(?=.*v=[\w-]+)(?:\S+)?$/); if (matches) { } else { error +="\nInvalid Youtube Url"; }
Sir Lojik
is there a typo in my query? how comes its failing here. i am starting to get a brain jam
Sir Lojik
it works... must have been a caching issue with js
Sir Lojik