JS matching URL of the regular expression set _ regular expression

Source: Internet
Author: User
Tags sub domain subdomain

DNS stipulates that the label in the domain name is composed of English letters and numbers, each label is not more than 63 characters, also does not distinguish between uppercase and lowercase letters. No other punctuation marks can be used except for hyphens (-) in the label. The lowest-level domain name is written on the leftmost, and the highest-level domain name is on the far right. A complete domain name consisting of multiple labels is no more than 255 characters in total. So the verification of the URL URLs can be the following several

Method One:

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

Method Two: Recommend

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-: 
  + ((/?) | "//A S Lash 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));

Just found the code for a nice multifunction test function:

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

After running, view effects through Chorme F12

The above is mainly described in the JS function of the writing and judging methods, the following is a small collation of some of the verification of the URL of the regular expression you can refer to the

Regular expressions
(HTTP|FTP|HTTPS): \/\/[\w\-_]+ (\.[ \w\-_]+) + ([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
The http://regxlib.com/Default.aspx | Http://electronics.cnet.com/electronics/0-6342366-8-8994967-1.html
does not match www.yahoo.com

Regular expressions
^\\{2}[\w-]+\\ ([\w-][\w-\s]*[\w-]+[$$]?$) | ( [\w-] [$$]?$))
The \\server\service | \\server\my Service | \\serv_001\service$
does not match \\my Server\service | \\server\ Service | \\server$\service

Regular expressions
^ (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\.\,\?\ ' \\/\+&%\$#\=~_\-@]*) *$
The http://www.sysrage.net | Https://64.81.85.161/site/file.php?cow=moo ' s |ftp://user:pass@host.com:123
does not match Sysrage.net

Regular expressions
^ ([a-za-z]\:|\\\\[^\/\\:*?) <>|] +\\[^\/\\:*?" <>|] +)(\\[^\/\\:*?" <>|] +)+(\. [^\/\\:*?" <>|] +)$
The C:\Test.txt | \\server\shared\Test.txt | \\server\shared\Test.t
does not match C:\Test | \\server\shared | \\server\shared\Test.?

Regular expressions
^ (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\.\,\?\ ' \\\+&%\$#\=~_\-]+) *$
The Http://site.com/dir/file.php?var=moo | Https://localhost |ftp://user:pass@site.com:21/file/dir
does not match site.com | http://site.com/dir//

Regular expressions
^ ([a-za-z]\:) (\\[^\\/:*?<> "|] * (? <! [ ]))*(\. [A-za-z] {2,6}) $
The C:\di___r\fi_sysle.txt | C:\dir\filename.txt
does not match C:\dir\file?name.txt

Regular expressions
^ ([a-za-z0-9] ([a-za-z0-9\-]{0,61}[a-za-z0-9]) +[a-za-z]{2,6}$
The regexlib.com | This.is.a.museum | 3com.com
does not match notadomain-.com | HELLOWORLD.C |. oops.org

Regular expressions
^ ((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\.\,\;\?\ ' \\\+&%\$#\=~_\-]+) *$
The www.blah.com:8103 | Www.blah.com/blah.asp?sort=ASC |www.blah.com/blah.htm#blah
does not match Www.state.ga | http://www.jb51.ru

regular expression
\b ([\w-]+://?| www[.]) [^\s () <>]+ (?: \ ([\w\d]+\) | ([^[:p unct:]\s]|/))
match 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/. |
no_ws.example.com | no_proto_or_ws.com |/relative_resource.php

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.