JS验证URL函数 正则

搜集一个JS验证URL函数,方便需要的朋友

这个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]\:)(\\[^\\/:*?<>"|]*(?
匹配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://0133.cn/blah_blah | http://0133.cn/blah_blah/ | (Something like http://0133.cn/blah_blah) | http://0133.cn/blah_blah_(wikipedia) | (Something like http://0133.cn/blah_blah_(wikipedia)) | http://0133.cn/blah_blah. |http://0133.cn/blah_blah/. | | | http://0133.cn/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

以上就是JS验证URL函数 正则的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 正则表达式