这个url的正则表达式判断的JavaScript!比较全面的。它验证的情况包括IP,域名(domain),ftp,二级域名,域名中的文件,域名加上端口!用户名等等信息,貌似作者也是在网上找的,我从一个项目代码中扣出来的,是我见过的最强最全面的url验证方式!太猛了,贴在这里与大家分享,URL的验证实在是很频繁。
function IsURL (str_url) { var strRegex = '^((https|http|ftp|rtsp|mms)?://)' + '?(([0-9a-z_!~*/'().&=+$%-]+: )?[0-9a-z_!~*/'().&=+$%-]+@)?' //ftp的user@ + '(([0-9]{1,3}.){3}[0-9]{1,3}' // IP形式的URL- 199.194.52.184 + '|' // 允许IP和DOMAIN(域名) + '([0-9a-z_!~*/'()-]+.)*' // 域名- www. + '([0-9a-z][0-9a-z-]{0,61})?[0-9a-z].' // 二级域名 + '[a-z]{2,6})' // first level domain- .com or .museum + '(:[0-9]{1,4})?' // 端口- :80 + '((/?)|' // a slash isn't required if there is no file name + '(/[0-9a-z_!~*/'().;?:@&=+$,%#-]+)+/?)$'; var re=new RegExp(strRegex); //re.test() if (re.test(str_url)) { return (true); } else { return (false); } }
代码二:
function CheckUrl(str) { var RegUrl = new RegExp(); RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+//.[A-Za-z0-9-_%&/?//.=]+$");//jihua.cnblogs.com if (!RegUrl.test(str)) { return false; } return true; }
代码三:
function checkUrl(urlString){if(urlString!=""){var reg=/(http|ftp|https):////[/w/-_]+(/.[/w/-_]+)+([/w/-/.,@?^=%&:/~/+#]*[/w/-/@?^=%&/~/+#])?/;if(!reg.test(urlString)){alert("不是正确的网址吧,请注意检查一下"); } }}
下面给大家分享一个常用的验证网址的正则表达式
正则表达式 | (http|ftp|https):////[/w/-_]+(/.[/w/-_]+)+([/w/-/.,@?^=%&:/~/+#]*[/w/-/@?^=%&/~/+#])? |
---|---|
匹配 | http://regxlib.com/Default.aspx | http://electronics.cnet.com/electronics/0-6342366-8-8994967-1.html |
不匹配 | www.yahoo.com |
正则表达式 | ^//{2}[/w-]+//(([/w-][/w-/s]*[/w-]+[$$]?$)|([/w-][$$]?$)) |
---|---|
匹配 | //server/service | //server/my service | //serv_001/service$ |
不匹配 | //my server/service | //server/ service | //server$/service |
正则表达式 | ^(http|https|ftp)/://([a-zA-Z0-9/./-]+(/:[a-zA-Z0-9/.&%/$/-]+)*@)?((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9/-]+/.)*[a-zA-Z0-9/-]+/.[a-zA-Z]{2,4})(/:[0-9]+)?(/[^/][a-zA-Z0-9/./,/?/'////+&%/$#/=~_/-@]*)*$ |
---|---|
匹配 | http://www.sysrage.net | https://64.81.85.161/site/file.php?cow=moo's |ftp://user:pass@host.com:123 |
不匹配 | sysrage.net |
正则表达式 | ^([a-zA-Z]/:|////[^////:*?"<>|]+//[^////:*?"<>|]+)(//[^////:*?"<>|]+)+(/.[^////:*?"<>|]+)$ |
---|---|
匹配 | c:/Test.txt | //server/shared/Test.txt | //server/shared/Test.t |
不匹配 | c:/Test | //server/shared | //server/shared/Test.? |
正则表达式 | ^(http|https|ftp)/://([a-zA-Z0-9/./-]+(/:[a-zA-Z0-9/.&%/$/-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)/.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9/-]+/.)*[a-zA-Z0-9/-]+/.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(/:[0-9]+)*(/($|[a-zA-Z0-9/./,/?/'///+&%/$#/=~_/-]+))*$ |
---|---|
匹配 | http://site.com/dir/file.php?var=moo | https://localhost |ftp://user:pass@site.com:21/file/dir |
不匹配 | site.com | http://site.com/dir// |
正则表达式 | ^([a-zA-Z]/:)(//[^///:*?<>"|]*(?<![ ]))*(/.[a-zA-Z]{2,6})$ |
---|---|
匹配 | C:/di___r/fi_sysle.txt | c:/dir/filename.txt |
不匹配 | c:/dir/file?name.txt |
正则表达式 | ^([a-zA-Z0-9]([a-zA-Z0-9/-]{0,61}[a-zA-Z0-9])?/.)+[a-zA-Z]{2,6}$ |
---|---|
匹配 | regexlib.com | this.is.a.museum | 3com.com |
不匹配 | notadomain-.com | helloworld.c | .oops.org |
正则表达式 | ^(((ht|f)tp(s?))/://)?(www.|[a-zA-Z].)[a-zA-Z0-9/-/.]+/.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(/:[0-9]+)*(/($|[a-zA-Z0-9/./,/;/?/'///+&%/$#/=~_/-]+))*$ |
---|---|
匹配 | www.blah.com:8103 | www.blah.com/blah.asp?sort=ASC |www.blah.com/blah.htm#blah |
不匹配 | www.state.ga | http://www.jb51.ru |
正则表达式 | /b(([/w-]+://?|www[.])[^/s()<>]+(?:/([/w/d]+/)|([^[:punct:]/s]|/))) |
---|---|
匹配 | http://VeVB.COm/blah_blah | http://VeVB.COm/blah_blah/ | (Something like http://VeVB.COm/blah_blah) | http://VeVB.COm/blah_blah_(wikipedia) | (Something like http://VeVB.COm/blah_blah_(wikipedia)) | http://VeVB.COm/blah_blah. |http://VeVB.COm/blah_blah/. | <http://VeVB.COm/blah_blah> | <http://VeVB.COm/blah_blah/>| http://VeVB.COm/blah_blah, | http://www.example.com/wpstyle/?p=364. | http://?df.ws/123 | rdar://1234 | rdar:/1234 | http://userid:password@example.com:8080 |http://userid@example.com | http://userid@example.com:8080 |http://userid:password@example.com |
不匹配 | no_ws.example.com | no_proto_or_ws.com | /relative_resource.php |
新闻热点
疑难解答
图片精选