Url正则表达式不正确

url regexp not proper

本文关键字:不正确 正则表达式 Url      更新时间:2023-09-26

我试图用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有什么问题?

我希望这些url通过regexp:

http://www.google.com
http://google.com
https://www.google.com
https://google.com
google.com
www.google.com

试试这个:

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

:

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)

regexp有什么问题?

regexp的这一部分:

([0-9A-Za-z]+'.)
在你的例子中

不能与"google.com"匹配。但它匹配错误的域名地址,如google。像这样:

(?:[0-9A-Za-z]+'.)+[A-Za-z]{2,}

在这个正则表达式中有几件事不起作用。例如,在最后的句号之后什么都没有发生。也不需要{1}作为默认情况下匹配一次的每个表达式。主要问题是|,因为分支不在()内,所以在每个|之后,比赛完全重新开始

这是您的regexp清理。例如,将内容分组,参见()中的|

isMatch = (/^(?:(?:https?|ftp):'/'/)?(?:www'.)?[0-9a-z]+'.[a-z]+/im.test(url))