有人可以帮助我使用正则表达式验证" url"以" http"开头或" https"像这样的例子:
http://www.url.com ,
http://url.com ,
http://domain.subdomain.com,
https://www.url.com ,
https://url.com ,
https://domain.subdomain.com
和没有http和https的网址就像这样的例子:
www.url.com
url.com
domain.subdomain.com
我有这个代码,它几乎完美无缺:
var regex = new RegExp("(http://|https://|)(www\b?|[a-zA-Z0-9]+[.]{1})([a- zA-Z0-9]+[.]{1}){1}[a-zA-z0-9]+(\\/{1}[a-zA-Z0-9]+)*\\/?$", "i");
但是如果你输入这个例子:
htttttp://wwww.address.com
hhtttttpp://wwww.address.com
httpp://wwww.address.com
有些像那样
表达式批准了网址,这是错误的,有人可以帮我这个吗?
答案 0 :(得分:-5)
document.body.innerHTML += validateUrl('http://www.google-com.123.com') + '<br />';
document.body.innerHTML += validateUrl('http://www.google-com.123') + '<br />';
document.body.innerHTML += validateUrl('https://www.google-com.com') + '<br />';
document.body.innerHTML += validateUrl('http://google-com.com') + '<br />';
document.body.innerHTML += validateUrl('http://google.com') + '<br />';
document.body.innerHTML += validateUrl('google.com') + '<br />';
document.body.innerHTML += validateUrl('htttttp://wwww.address.com') + '<br />';
document.body.innerHTML += validateUrl('hhtttttpp://wwww.address.com') + '<br />';
document.body.innerHTML += validateUrl('httpp://wwww.address.com') + '<br />';
function validateUrl(value){
return /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([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