jQuery Youtube URL 验证与正则表达式 [英] jQuery Youtube URL Validation with regex

查看:38
本文介绍了jQuery Youtube URL 验证与正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有很多问题的答案https://stackoverflow.com/questions/tagged/youtube+regex,但找不到与我相似的问题.

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

任何正文都有用于验证下面列出的 YouTube 视频 URL 行的 JavaScript 正则表达式.只想知道哪里可以有这样的网址

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

-- 更新 1---- 更新 2--

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

这个几乎可以正常工作,但对于 URL http://youtube.com/watch?v 失败了=bQVoAWSP7k4

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');
}

推荐答案

^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+)?$

这篇关于jQuery Youtube URL 验证与正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆