开发者

Youtube url tester with hyphens

开发者 https://www.devze.com 2023-01-22 06:41 出处:网络
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 htt开发者_高级运维p://www.youtube.com/watch?v=-pIaQpwYEjY

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 htt开发者_高级运维p://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";
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消