Javascript 用于网站或 url 验证的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42618872/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regex for website or url validation
提问by Dheeraj Agrawal
I know this is so common question & there are so many answers for this, but my problem is different, I want to accept the URL with all below formats:
我知道这是一个很常见的问题,对此有很多答案,但我的问题不同,我想接受以下所有格式的 URL:
http://www.sample.com
https://www.sample.com
http://www.sample.com/xyz
www.sample.com
www.sample.com/xyz/#/xyz
sample.com
// & much more ...
So here idea is, I am allowing the user to enter there website & another user can just click on the saved website & go to users website. So here it's not a good idea to restrict the user to input proper URL, they can enter any of the above formats. Here is the regex I am using right now, but that only checks if URL starts from proper protocol:
所以这里的想法是,我允许用户进入那里的网站,另一个用户可以点击保存的网站并转到用户网站。所以在这里限制用户输入正确的 URL 不是一个好主意,他们可以输入上述任何格式。这是我现在使用的正则表达式,但它只检查 URL 是否从正确的协议开始:
^(ftp|http|https):\/\/[^ "]+$
回答by Sagar V
Use the regex ^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+\.[a-z]+(\/[a-zA-Z0-9#]+\/?)*$
使用正则表达式 ^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+\.[a-z]+(\/[a-zA-Z0-9#]+\/?)*$
This is a basic one I build just now. A google search can give you more.
这是我刚刚构建的一个基本的。谷歌搜索可以给你更多。
Here
这里
- ^ Should start with
- ((https?|ftp|smtp)://)? may or maynot contain any of these protocols
- (www.)? may or may not have www.
- [a-z0-9]+(.[a-z]+) url and domain and also subdomain if any upto 2 levels
- (/[a-zA-Z0-9#]+/?)*/? can contain path to files but not necessary. last may contain a
/ - $ should end there
- ^ 应该以
- ((https?|ftp|smtp)://)?可能包含也可能不包含任何这些协议
- (万维网。)?可能有也可能没有 www。
- [a-z0-9]+(.[az]+) url 和域以及子域(如果有)最多 2 级
- (/[a-zA-Z0-9#]+/?)*/? 可以包含文件路径但不是必需的。最后可能包含一个
/ - $ 应该在那里结束
var a=["http://www.sample.com","https://www.sample.com/","https://www.sample.com#","http://www.sample.com/xyz","http://www.sample.com/#xyz","www.sample.com","www.sample.com/xyz/#/xyz","sample.com","sample.com?name=foo","http://www.sample.com#xyz","http://www.sample.c"];
var re=/^((https?|ftp|smtp):\/\/)?(www.)?[a-z0-9]+(\.[a-z]{2,}){1,3}(#?\/?[a-zA-Z0-9#]+)*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/;
a.map(x=>console.log(x+" => "+re.test(x)));
回答by Rizwan M.Tuman
You can try this:
你可以试试这个:
^((ftp|http|https):\/\/)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+((\/)[\w#]+)*(\/\w+\?[a-zA-Z0-9_]+=\w+(&[a-zA-Z0-9_]+=\w+)*)?$
const regex = /^((ftp|http|https):\/\/)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+((\/)[\w#]+)*(\/\w+\?[a-zA-Z0-9_]+=\w+(&[a-zA-Z0-9_]+=\w+)*)?$/gm;
const str = `http://www.sample.com
https://www.sample.com
http://www.sample.com/xyz
www.sample.com
www.sample.com/xyz/#/xyz
sample.com
www.sample.com
mofiz.com
kolim.com
www.murikhao.www.sample.com
http://murihao.www.sample.com
http://www.sample.com/xyz?abc=dkd&p=q&c=2
www.sample.gov.bd
www.sample.com.en
www.sample.vu
`;
let m;
while ((m = regex.exec(str)) !== null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
console.log("matched :"+m[0]);
}

