youtube URL 的正则表达式 [英] Regex for youtube URL
本文介绍了youtube URL 的正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用以下正则表达式来验证 youtube 视频 share 网址.
I am using the following regex for validating youtube video share url's.
var valid = /^(http\:\/\/)?(youtube\.com|youtu\.be)+$/;
alert(valid.test(url));
return false;
我希望正则表达式支持以下 URL 格式:
I want the regex to support the following URL formats:
http://youtu.be/cCnrX1w5luM
http://youtube/cCnrX1w5luM
www.youtube.com/cCnrX1w5luM
youtube/cCnrX1w5luM
youtu.be/cCnrX1w5luM
我尝试了不同的正则表达式,但没有找到适合共享链接的正则表达式.谁能帮我解决这个问题.提前致谢.
I tried different regex but I am not getting a suitable one for share links. Can anyone help me to solve this. Thanks in advance.
推荐答案
- 您的正则表达式中缺少
www
- 第二个
\.
应该是可选的(因为youtu.be
和youtube
都是有效的) 正则表达式中的 +
允许一个或多个(youtube\.com|youtu\.be)
,而不是一个或多个通配符
您需要使用.
表示通配符,并使用+
表示您想要其中的一个或多个.- You're missing
www
in your regex - The second
\.
should optional (since bothyoutu.be
andyoutube
are valid) +
in your regex allows for one or more of(youtube\.com|youtu\.be)
, not one or more wild-cards
You need to use a.
to indicate a wild-card, and+
to indicate you want one or more of them.
试试:
^(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/.+$
测试.
如果您希望它匹配带有或不带有 www.
的 URL,只需将其设为可选:
If you want it to match URLs with or without the www.
, just make it optional:
^(https?\:\/\/)?((www\.)?youtube\.com|youtu\.?be)\/.+$
如果您希望 www.youtu.be/...
也匹配,请将可选的 www.
放在括号外:
If you want www.youtu.be/...
to also match, put the optional www.
outside the brackets:
^(https?\:\/\/)?(www\.)?(youtube\.com|youtu\.?be)\/.+$
这篇关于youtube URL 的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文