【发布时间】:2020-06-22 13:14:11
【问题描述】:
我正在尝试验证以“http://”或“https://”开头的字符串。一些例子:
http://example.com -> 好
https://example.com -> 好
http:///example.com -> 错误
http:/www.example.com -> 错误
https//example.com -> 错误
我有这个正则表达式,但是效果不好:
str.match(/^(http|https):\/\/?[a-d]/);
...有什么帮助吗?
【问题讨论】:
-
试试
str.match(/^https?:\/\/(?!\/)/i);或str.match(/^https?:\/\/\b/i); -
请问为什么域名必须以
[a-d]开头?在您的示例中,您想接受w之后的d但您的正则表达式只接受a-d
标签: javascript regex url url-validation