JS regular expression validation URL function code (convenient for multiple regular comparisons) _ Regular expressions

Source: Internet
Author: User

Recommend the collection of a piece of code to facilitate the simultaneous testing of multiple regular, viewing different test results, combined with chrome perfect

Core code

<script>/** * Regular expression to determine whether the URL is valid/(function () {"Use strict";
    var urldict=[//bad case ' www.baidu.com ',//regular URL, without the address of the protocol header ' w.baidu.com ',//regular URL, short sub domain name
    ' baidu.com ',//regular URL, only the main domain name ' test. com ',//unconventional legal URL, the Chinese domain name is not referenced in the list of ' 1.2 ',//Wrong domain name ' Wwww ',//invalid string ' 111 test ',//Invalid string//correct case ' http://baidu.com ',//General URL, only the main domain name ' http://www.baidu.com ',//regular URL, belt domain name ' https://www.baidu.com/',//regular URL, use HTTPS protocol header, with root directory ' HT Tp://www.baidu.com/api ',//Regular web site, there is a directory of resources ' Http://www.subdomain.baidu.com/index/subdir ',//Regular web site, multilevel subdomain, multilevel directory ' http://www.www.subdomain.baidu.com/index/subdir/',//regular URL, multi-level subdomain, multi-level directory, directory address closed ' Http://io.io '//unconventional URL, multilevel sub-domain, multilevel mesh
 
  Record, directory address closed]; Suggested regular function Isurl (str) {return!! Str.match ((^https:(?: \ /\/)?) (?: [-;:&=\+\$,\w]+@)? [a-za-z0-9.-]+| (?: www.| [-;:&=\+\$,\w]+@) [a-za-z0-9.-]+] ((?: \ /[\+~%\/. \w-_]*)??? (?: [-\+=&;%@.\w_]*) #? (?: [\w]*)]?
  $/G); //Don't know who wrote the simple version of the Pit Dad regular 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 form URL-199.194.52.184 + |"//Allow IP and domain (domain name) + "([0-9a-z_!~* ' ()-]+\.) 
      * "//Domain name-www." + "([0-9a-z][0-9a-z-]{0,61})?" [0-9a-z]\.] Level two domain + "[a-z]{2,6}]"///domain-. com or. Museum + "(: [0-9]{1,4})?"//Port-: 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); 
    }///test Case Overlay (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>

Debug Method:

Save the above code as test.htm run in Chrome and open F12 to see the effect as shown in the following image

Read the above is mainly some of the detection URL of the regular, you can refer to this article: http://www.jb51.net/article/31550.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.