English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Regular Expression URL Validation Function Code (Convenient for Comparison of Multiple Regular Expressions)

Recommended for everyone to save a piece of code, convenient for testing multiple regular expressions at the same time, viewing different results, and perfectly combined with Chrome

Core code

<script>
/**
 * Regular expression to judge if the website is valid
 */
(function(){
  "use strict";
  var urlDict=[
    //Bad Case
    "www.baidu.com',           //Conventional URL, address without protocol header
    "w.baidu.com',            //Conventional URL, short subdomain
    "baidu.com',             //Conventional URL, only the main domain
    "test.com',              //Unconventional valid URL, Chinese domain is not included in the reference.
    1.2',                //Error domain
    " WWWW ",              //Invalid string
    111Test',              //Invalid string
    //Correct Case
    "http://baidu.com',          //Conventional URL, only the main domain
    "http://www.baidu.com',        //Conventional URL, with subdomain
    "https://www.baidu.com/',       //Conventional URL, using https protocol header, with root directory
    "http://www.baidu.com/api',      //Conventional URL, there are resources under a level directory
    "http://www.subdomain.baidu.com/index/subdir',   //Conventional URL, multiple level subdomains, multiple level directories
    "http://www.www.subdomain.baidu.com/index/subdir/',//Conventional URL, multiple level subdomains, multiple level directories, directory address is closed
    "http://io.io'            //Unconventional URL, multiple level subdomains, multiple level directories, directory address is closed
  ];
  // Suggested regular expression
  function isURL(str){
    return !!str.match(/(((^https?:(?:\/\/)?)(?:-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\?\?(?:-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/g);
  }
  // A simple and坑爹 regular expression written by who knows.
  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\s*user@ 
      + "(\[0-9]{1,3}\.)\{3}[0-9]{1,3"} // URL in IP form- 199.194.52.184 
      + "|" // Allow IP and DOMAIN (domain)
      + "([0-9a-z_!~*' ()-]+\.)"}*" // domain name- www. 
      + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // second level domain 
      + "[a-z]{2,6})" // first level 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 Coverage
  (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>

Debugging Method:

Save the above code as test.htm and run it in Chrome, open F12, and you can see the effect as shown in the figure below

The main content above is mainly some regular expressions for detecting URLs, everyone can refer to this article: https://www.oldtoolbag.com/article/31550.htm

You May Also Like