欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

js匹配網(wǎng)址url的正則表達式集合

 更新時間:2016年10月31日 21:17:01   投稿:mdxy-dxy  
今天在開發(fā)客戶端url驗證的時候,整理的代碼,發(fā)現(xiàn)了一些不錯的匹配url網(wǎng)址的正則表達式特整理一下,方便需要的朋友

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

方法一:

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

方法二:推薦

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

剛發(fā)現(xiàn)一個不錯的多功能測試函數(shù)的代碼:

<script>
/**
 * 正則表達式判斷網(wǎng)址是否有效
 */
 
(function(){
  "use strict";
 
  var urlDict=[
    //Bad Case
    'www.baidu.com',           //常規(guī)網(wǎng)址,未帶協(xié)議頭的地址
    'w.baidu.com',            //常規(guī)網(wǎng)址,短子域名
    'baidu.com',             //常規(guī)網(wǎng)址,僅有主域名
    '測試.com',              //非常規(guī)合法網(wǎng)址,中文域名不在參考之列
    '1.2',                //錯誤域名
    ' WWWW ',              //無效字符串
    '111測試',              //無效字符串
    //Correct Case
    'http://baidu.com',          //常規(guī)網(wǎng)址,僅有主域名
    'http://www.baidu.com',        //常規(guī)網(wǎng)址,帶子域名
    'https://www.baidu.com/',       //常規(guī)網(wǎng)址,使用https協(xié)議頭,帶根目錄
    'http://www.baidu.com/api',      //常規(guī)網(wǎng)址,有一級目錄下資源
    'http://www.subdomain.baidu.com/index/subdir',   //常規(guī)網(wǎng)址,多級子域名,多級目錄
    'http://www.www.subdomain.baidu.com/index/subdir/',//常規(guī)網(wǎng)址,多級子域名,多級目錄,目錄地址閉合
    'http://io.io'            //非常規(guī)網(wǎng)址,多級子域名,多級目錄,目錄地址閉合
  ];
 
  // 建議的正則
  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函數(shù)的寫法與判斷方法,下面是小編整理的一些關于驗證網(wǎng)址的正則表達式大家可以參考一下

正則表達式
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&amp;:/~\+#]*[\w\-\@?^=%&amp;/~\+#])?
匹配 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\.&amp;%\$\-]+)*@)?((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\.\,\?\'\\/\+&amp;%\$#\=~_\-@]*)*$
匹配 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\.&amp;%\$\-]+)*@)*((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\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$
匹配 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\.\,\;\?\'\\\+&amp;%\$#\=~_\-]+))*$
匹配 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://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

相關文章

最新評論