js匹配網址url的Regex集合_Regex

來源:互聯網
上載者:User

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.jb51.net/article/1.htm";alert(IsURL(testUrl));

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

<script>/** * Regex判斷網址是否有效 */ (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);  }//jb51function 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函數的寫法與判斷方法,下面是小編整理的一些關於驗證網址的Regex大家可以參考一下

Regex
(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

Regex
^\\{2}[\w-]+\\(([\w-][\w-\s]*[\w-]+[$$]?$)|([\w-][$$]?$))
匹配 \\server\service | \\server\my service | \\serv_001\service$
不匹配 \\my server\service | \\server\ service | \\server$\service

Regex
^(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

Regex
^([a-zA-Z]\:|\\\\[^\/\\:*?"<>|]+\\[^\/\\:*?"<>|]+)(\\[^\/\\:*?"<>|]+)+(\.[^\/\\:*?"<>|]+)$
匹配 c:\Test.txt | \\server\shared\Test.txt | \\server\shared\Test.t
不匹配 c:\Test | \\server\shared | \\server\shared\Test.?

Regex
^(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//

Regex
^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$
匹配 C:\di___r\fi_sysle.txt | c:\dir\filename.txt
不匹配 c:\dir\file?name.txt

Regex
^([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

Regex
^(((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

Regex
\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
匹配 http://jb51.net/blah_blah | http://jb51.net/blah_blah/ | (Something like http://jb51.net/blah_blah) | http://jb51.net/blah_blah_(wikipedia) | (Something like http://jb51.net/blah_blah_(wikipedia)) | http://jb51.net/blah_blah. |http://jb51.net/blah_blah/. | <http://jb51.net/blah_blah> | <http://jb51.net/blah_blah/>| http://jb51.net/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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.