我尝试使用js验证URL。
function validateURL(url) {
var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.|https:\/\/|http:\/\/){1}([0-9A-Za-z]+\.)");
return urlregex.test(url);
}
但是我希望google.com
也会通过,但它不会通过这个正则表达式。
regexp有什么问题?
我希望这些网址通过regexp传递:
http://www.google.com
http://google.com
https://www.google.com
https://google.com
google.com
www.google.com
答案 0 :(得分:2)
请改为尝试:
function validateURL(url) {
var urlregex = new RegExp("^((?:https?:\/\/)?(?:www\.)?[0-9A-Za-z]+\.[0-9A-Za-z]+)$");
return urlregex.test(url);
}
“
<强>样本强>
http://regex101.com/r/rG8wP9
<强> OR: 强>
function validateURL(url) {
if (/^((?:https?:\/\/)?(?:www\.)?[0-9A-Za-z]+\.[0-9A-Za-z]+)$/im.test(url)) {
return true;
} else {
return false;
}
}
说明:
^ assert position at start of a line
1st Capturing group ((?:https?:\/\/)?(?:www\.)?[0-9A-Za-z]+\.[0-9A-Za-z]+)
(?:https?:\/\/)? Non-capturing group
Quantifier: Between zero and one time, as many times as possible, giving back as needed [greedy]
http matches the characters http literally (case sensitive)
s? matches the character s literally (case sensitive)
Quantifier: Between zero and one time, as many times as possible, giving back as needed [greedy]
: matches the character : literally
\/ matches the character / literally
\/ matches the character / literally
(?:www\.)? Non-capturing group
Quantifier: Between zero and one time, as many times as possible, giving back as needed [greedy]
www matches the characters www literally (case sensitive)
\. matches the character . literally
[0-9A-Za-z]+ match a single character present in the list below
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
A-Z a single character in the range between A and Z (case sensitive)
a-z a single character in the range between a and z (case sensitive)
\. matches the character . literally
[0-9A-Za-z]+ match a single character present in the list below
Quantifier: Between one and unlimited times, as many times as possible, giving back as needed [greedy]
0-9 a single character in the range between 0 and 9
A-Z a single character in the range between A and Z (case sensitive)
a-z a single character in the range between a and z (case sensitive)
$ assert position at end of a line
g modifier: global. All matches (don't return on first match)
m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
答案 1 :(得分:0)
regexp有什么问题?
这部分正则表达式:
([0-9A-Za-z]+\.)
在您的示例中,无法与“google.com”匹配。但它与错误的域名地址匹配,例如谷歌。这样的事情:
(?:[0-9A-Za-z]+\.)+[A-Za-z]{2,}
更加正确和灵活。
答案 2 :(得分:0)
这个正则表达式中有些功能无效。例如,在结束之后没有任何事情发生。也绝不需要{1}
,因为默认情况下每个表达式匹配一次。主要问题是|
因为分支不在()
内,所以在每个|
之后匹配重新开始
这是你的正则表达式清理。例如,将内容分组,请参阅|
内的()
。
isMatch = (/^(?:(?:https?|ftp):\/\/)?(?:www\.)?[0-9a-z]+\.[a-z]+/im.test(url))