A set of Regular Expressions matching URLs in js.

Source: Internet
Author: User
Tags subdomain subdomain name

A set of Regular Expressions matching URLs in js.

According to DNS rules, the labels in a domain name are composed of English letters and numbers. Each label cannot exceed 63 characters and is not case sensitive. You cannot use other punctuation marks except hyphens. The lowest-level domain names are written on the leftmost, while the highest-level domain names are written on the rightmost. A complete domain name composed of multiple labels cannot exceed 255 characters. For verification, the url RegEx 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 ("the URL is not correct. Check ");}}}

Method 2: Recommended

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} "// URL in IP Format-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] \. "// second-level 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 = "http://www.bkjia.com/article/1.htm "; alert (IsURL (testUrl ));

I just found a good multi-function test function code:

<Script>/*** regular expression to determine whether the URL is valid */(function () {"use strict"; var urlDict = [// Bad Case 'www .baidu.com ', // regular URL, 'w .baidu.com' without a protocol header, // regular URL, short subdomain name 'Baidu. com ', // regular URL, only the primary domain name' test. com ', // non-conventional legal URL, Chinese domain name is not in the reference column '1. 2', // error domain name 'www', // invalid string '2017 test', // invalid string // Correct case' http://baidu.com ', // Regular URL, only the primary domain name' http://www.baidu.com ', // Regular URL, with domain name' https://www.baidu.com/ ', // Regular URL, using the https header, with the root directory' http://www.baidu.com /Api ', // regular URL, with resources under a level-1 directory' http://www.subdomain.baidu.com/index/subdir ', // Regular URL, multi-level subdomain name, multi-level directory' http://www.www.subdomain.baidu.com/index/subdir/ ', // Regular URL, multi-level subdomain name, multi-level directory, and directory address closure' http://io.io '// Unconventional URL, multi-level subdomain name, multi-level directory, directory address closed]; // recommended regular function isURL (str) {return !! Str. match (/(^ https? :(? :\/\/)?) (?: [-;: & =\+ \$, \ W] + @)? [A-Za-z0-9.-] + | (?: Www. | [-;: & =\+ \ $, \ w] + @) [A-Za-z0-9.-] + )((?: \/[\ + ~ % \/. \ W-_] *)? \?? (?: [-\ + = &; % @. \ W _] *) #? (?: [\ W] *)?) $/G) ;}// I don't know who wrote the simple version of the potholes regular function badRegFn (str) {return !! Str. match (/(http [s]? | Ftp): \/[^ \/\.] +? \ .. + \ W $/g);} // jb51function 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} "// URL in IP Format-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] \. "// second-level 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 overwrite (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 the command, run the chorme F12 command to check the effect.

The above section mainly describes the writing and judgment methods of js functions. Below are some of the regular expressions for verifying URLs.

Regular Expression (Http | ftp | https): \/[\ w \-_] + (\. [\ w \-_] +) + ([\ w \-\., @? ^ = % &:/~ \ + #] * [\ W \-\@? ^ = % &/~ \ + #])?
Match 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-] [$]? $ ))
Match \ Server \ service | \ server \ my service | \ serv_001 \ service $
Mismatch \ My 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 \.\,\? \ '\/\ + & % \ $ # \ = ~ _ \-@] *) * $
Match Http://www.sysrage.net | https: // 64.81.85.161/site/file. php? Cow = moo's | ftp: // user: pass@host.com: 123
Mismatch Sysrage.net

Regular Expression ^ ([A-zA-Z] \:|\\\ [^ \/\\:*? "<> |] + \ [^ \/\\:*? "<> |] +) (\ [^ \/\\:*? "<> |] +) + (\. [^ \/\\:*? "<> |] +) $
Match C: \ Test.txt | \ server \ shared \ Test. t
Mismatch C: \ Test | \ 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 | Rpa | info | name | pro | aero | coop | museum | [a-zA-Z] {2}) (\: [0-9] +) * (/($ | [a-zA-Z0-9 \. \,\? \ '\\\+ & %\# #\= ~ _ \-] +) * $
Match Http://site.com/dir/file.php? Var = moo | https: // localhost | ftp: // user: pass@site.com: 21/file/dir
Mismatch Site.com | http://site.com/dir //

Regular Expression ^ ([A-zA-Z] \ :) (\ [^ \\/:*? <> "|] * (? <! []) * (\. [A-zA-Z] {2, 6}) $
Match 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 \-] {} [a-zA-Z0-9])? \.) + [A-zA-Z] {2, 6} $
Match 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 \. \,\;\? \ '\\\+ & %\# #\= ~ _ \-] +) * $
Match Www.blah.com: 8103 | www.blah.com/blah.asp? Sort = ASC | www.blh.com/blh.htm?blah
Mismatch Www. state. ga | http://www.jb51.ru

Regular Expression \ B ([\ w-] + ://? | Www [.]) [^ \ s () <>] + (? : \ ([\ W \ d] + \) | ([^ [: punct:] \ 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. |
Mismatch 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.