a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > regex > 正文

js匹配網址url的正則表達式集合

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

DNS規定,域名中的標號都由英文字母和數字組成,每一個標號不超過63個字符,也不區分大小寫字母。標號中除連字符(-)外不能使用其他的標點符號。級別最低的域名寫在最左邊,而級別最高的域名寫在最右邊。由多個標號組成的完整域名總共不超過255個字符。所以驗證則網址url的正則可以如下幾種

方法一:

function checkUrl(urlString){if(urlString!=""){var reg=/(http|ftp|https):////[/w/-_]+(/.[/w/-_]+)+([/w/-/.,@?^=%&:/~/+#]*[/w/-/@?^=%&/~/+#])?/;if(!reg.test(urlString)){alert("不是正確的網址吧,請注意檢查一下");						}					}}

方法二:推薦

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);   } }var testUrl;testUrl="http://harveyzeng.iteye.com/blog/1776991";//var testUrl="http://www.5lwq4hdr.cn/article/1.htm";alert(IsURL(testUrl));

剛發現一個不錯的多功能測試函數的代碼:

<script>/** * 正則表達式判斷網址是否有效 */ (function(){  "use strict";   var urlDict=[    //Bad Case    'www.baidu.com',           //常規網址,未帶協議頭的地址    'w.baidu.com',            //常規網址,短子域名    'baidu.com',             //常規網址,僅有主域名    '測試.com',              //非常規合法網址,中文域名不在參考之列    '1.2',                //錯誤域名    ' WWWW ',              //無效字符串    '111測試',              //無效字符串    //Correct Case    'http://baidu.com',          //常規網址,僅有主域名    'http://www.baidu.com',        //常規網址,帶子域名    'https://www.baidu.com/',       //常規網址,使用https協議頭,帶根目錄    'http://www.baidu.com/api',      //常規網址,有一級目錄下資源    'http://www.subdomain.baidu.com/index/subdir',   //常規網址,多級子域名,多級目錄    'http://www.www.subdomain.baidu.com/index/subdir/',//常規網址,多級子域名,多級目錄,目錄地址閉合    'http://io.io'            //非常規網址,多級子域名,多級目錄,目錄地址閉合  ];   // 建議的正則  function isURL(str){    return !!str.match(/(((^https?:(?:////)?)(?:[-;:&=/+/$,/w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=/+/$,/w]+@)[A-Za-z0-9.-]+)((?://[/+~%//./w-_]*)?/??(?:[-/+=&;%@./w_]*)#?(?:[/w]*))?)$/g);  }   // 不知道誰寫的簡單版的坑爹正則  function badRegFn(str){    return !!str.match(/(http[s]?|ftp):////[^///.]+?/..+/w$/g);  }	//jb51	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(){    var ret={};     var collect=function(link){      var obj={},fnList=[isURL,badRegFn,IsURL];      for(var i=0,j=fnList.length;i<j;i++){        var fn=fnList[i];        obj[fn.name]=fn.call(null,link);      }      return obj;    };     for(var i=0,j=urlDict.length;i<j;i++){      ret[urlDict[i]]=collect(urlDict[i]);    }     console.log(ret),console.table(ret);  }()); }());</script>

運行以后通過chorme的F12查看效果

上面介紹的主要是js函數的寫法與判斷方法,下面是小編整理的一些關于驗證網址的正則表達式大家可以參考一下

正則表達式
(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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产日韩一区二区 | 中文字幕在线视频免费播放 | 91精品久久久久久综合五月天 | www.欧美日韩 | 男人的天堂久久 | 91资源在线观看 | 蜜桃久久久 | 亚洲国产欧美一区二区三区久久 | 久久激情视频 | 精品国产乱码久久久久久闺蜜 | 欧美日韩色图 | 午夜看片在线观看 | 日本一区二区三区四区不卡视频 | 亚洲精品在线播放视频 | 性大毛片视频 | 日韩欧美中文在线观看 | 日日摸夜夜添夜夜添亚洲女人 | 91精品国产高清自在线观看 | 午夜影视 | 精品国产乱码久久久久久久软件 | 天天干人人干 | 日韩欧美国产精品综合嫩v 狠狠综合久久 | 青娱乐国产在线 | 手机看片麻豆 | 中文字幕三区 | 黄色影院免费观看 | 国产精品免费视频观看 | 国产精品欧美日韩在线观看 | 涩综合 | 久久精品日产高清版的功能介绍 | 免费黄色特级片 | 国产视频第一页 | 国产乱码一二三区精品 | 亚洲国产精品久久久 | 日韩天堂 | 久久久久久久国产精品 | 国产精品国产自产拍高清av | 黄色毛片av | 99re视频在线播放 | 91在线播| 精品99久久久久久 |