JS matching URL regular expression set

Time:2019-10-5

DNS stipulates that all the labels in a domain name are composed of letters and numbers in English. Each label does not exceed 63 characters and does not distinguish between upper and lower case letters. No punctuation except hyphens (-) can be used in labels. The lowest level domain name is written on the leftmost side, while the highest level domain name is written on the rightmost side. A complete domain name consisting of multiple labels does not exceed 255 characters in total. So the rules of URL verification can be as follows

Method 1:

function checkUrl(urlString){
if(urlString!=""){
var reg=/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
if(!reg.test(urlString)){
Alert ("Not the right website, please check it");
						}
					}
}

Method 2: Recommendation

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]"//secondary domain name 
  + "[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); 
  }
 }
var testUrl;
testUrl="http://harveyzeng.iteye.com/blog/1776991";
//var testUrl="//www.jb51.net/article/1.htm";
alert(IsURL(testUrl));

Just found a good multi-functional test function code:

<script>
/**
 * Regular expressions to determine whether a web site is valid
 */
 
(function(){
  "use strict";
 
  var urlDict=[
    //Bad Case
    'www.baidu.com', //regular website, address without protocol header
    'w.baidu.com', // regular website, short subdomain name
    'baidu.com', // regular website, only main domain name
    'test.Com', // unconventional legal web address. Chinese domain name is not in the list of references.
    '1.2', // incorrect domain name
    'WWWW', // Invalid String
    '111 test', // invalid string
    //Correct Case
    'http://baidu.com', //regular website, only main domain name
    'http://www.baidu.com', //regular website, with subdomain names
    'https://www.baidu.com/', //regular website, using HTTPS protocol header, with root directory
    'http://www.baidu.com/api', //regular web site, with resources under a first-level directory
    'http://www.subdomain.baidu.com/index/subdir', //regular website, multi-level subdomain, multi-level directory
    'http://www.subdomain.baidu.com/index/subdir/', //regular web site, multi-level sub-domain name, multi-level directory, directory address closed
    'http://io.io'// Unconventional Web Sites, Multilevel Subdomain Names, Multilevel Directories, Closed Directory Addresses
  ];
 
  // Recommended Rules
  function isURL(str){
    return !!str.match(/(((^https?:(?:\/\/)?)(?:[-;:&=\+$,\w][email protected])?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+$,\w][email protected])[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/g);
  }
 
  // I don't know who wrote the simple version of pits.
  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]"//secondary domain name 
      + "[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>

After running, check the effect by chorme’s F12

The above is mainly about how to write and judge the JS function. Here are some regular expressions for verifying web sites that you can refer to.

regular expression
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
matching http://regxlib.com/Default.aspx | http://electronics.cnet.com/electronics/0-6342366-8-8994967-1.html
Mismatch www.yahoo.com

regular expression
^\\{2}[\w-]+\\(([\w-][\w-\s]*[\w-]+[$$]?$)|([\w-][$$]?$))
matching \\server\service | \\server\my service | \\serv_001\service$
Mismatch \\my server\service | \\server\ service | \\server$\service

regular expression
^(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\.\,\?\’\\/\+&%\$#\=~_\[email protected]]*)*$
matching http://www.sysrage.net | https://64.81.85.161/site/file.php?cow=moo’s |ftp://user:[email protected]:123
Mismatch sysrage.net

regular expression
^([a-zA-Z]\:|\\\\[^\/\\:*?”<>|]+\\[^\/\\:*?”<>|]+)(\\[^\/\\:*?”<>|]+)+(\.[^\/\\:*?”<>|]+)$
matching c:\Test.txt | \\server\shared\Test.txt | \\server\shared\Test.t
Mismatch c:\Test | \\server\shared | \\server\shared\Test.?

regular expression
^(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\.\,\?\’\\\+&%\$#\=~_\-]+))*$
matching http://site.com/dir/file.php?var=moo | https://localhost |ftp://user:[email protected]:21/file/dir
Mismatch site.com | http://site.com/dir//

regular expression
^([a-zA-Z]\:)(\\[^\\/:*?<>”|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$
matching C:\di___r\fi_sysle.txt | c:\dir\filename.txt
Mismatch c:\dir\file?name.txt

regular expression
^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$
matching regexlib.com | this.is.a.museum | 3com.com
Mismatch notadomain-.com | helloworld.c | .oops.org

regular expression
^(((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\.\,\;\?\’\\\+&%\$#\=~_\-]+))*$
matching www.blah.com:8103 | www.blah.com/blah.asp?sort=ASC |www.blah.com/blah.htm#blah
Mismatch www.state.ga | http://www.jb51.ru

regular expression
\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
matching 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:[email protected]:8080 |http://[email protected] | http://[email protected]:8080 |http://userid:[email protected]
Mismatch no_ws.example.com | no_proto_or_ws.com | /relative_resource.php