Url正则表达式不正确
url regexp not proper
我试图用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))
相关文章:
- 使用不带正则表达式的jscript验证电子邮件地址
- 不使用正则表达式的字符串字数
- 服务器错误消息的正确正则表达式
- 只允许整数和十进制数字textbow jquery(不带正则表达式)
- 如何在不使用正则表达式的情况下从 JavaScript 中的字符串中删除 HTML 标记
- 使用 javascript 查找文本中 HTML 标签的位置,最好不使用正则表达式
- 为什么这个JavaScript不能取代工作?(不包含正则表达式)
- Javascript:如何不将正则表达式与BB代码混淆
- 仅包含有效字符的字符串,不带正则表达式
- 不使用正则表达式的表单密码验证
- 不带正则表达式的JavaScript titleCase函数
- 如何在不使用正则表达式的情况下使用javascript在数组中搜索字符串
- 包含可选子字符串的正确正则表达式匹配
- 我可以显示普通字符而不是正则表达式吗?
- PHP正则表达式不工作,返回主题字符串而不是正则表达式结果
- 如何使用或不使用正则表达式获得两个花括号之间的所有内容
- 不匹配正则表达式
- 如何在不使用正则表达式的情况下在javascript中剥离HTML
标签
- 如果子字符串不在正则表达式中,则用双引号括起来
- 字符串解密:不匹配)正则表达式