国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > regex > 正文

JS驗證URL函數 正則

2020-01-20 22:14:18
字體:
來源:轉載
供稿:網友

這個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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 台南市| 渝中区| 亚东县| 林口县| 台山市| 城步| 犍为县| 普兰县| 谷城县| 清涧县| 二手房| 包头市| 苏尼特左旗| 深水埗区| 湖州市| 铜川市| 承德县| 克拉玛依市| 诸城市| 绍兴市| 丹棱县| 桦南县| 郸城县| 玉田县| 灵武市| 南靖县| 济宁市| 芒康县| 青浦区| 翼城县| 余江县| 德兴市| 铁力市| 彰化县| 华池县| 栾川县| 西充县| 泸州市| 平乡县| 谢通门县| 托克逊县|