DNS規(guī)定,域名中的標(biāo)號(hào)都由英文字母和數(shù)字組成,每一個(gè)標(biāo)號(hào)不超過(guò)63個(gè)字符,也不區(qū)分大小寫字母。標(biāo)號(hào)中除連字符(-)外不能使用其他的標(biāo)點(diǎn)符號(hào)。級(jí)別最低的域名寫在最左邊,而級(jí)別最高的域名寫在最右邊。由多個(gè)標(biāo)號(hào)組成的完整域名總共不超過(guò)255個(gè)字符。所以驗(yàn)證則網(wǎng)址url的正則可以如下幾種
方法一:
1
2
3
4
5
6
7
8
|
function checkUrl(urlString){ if (urlString!= "" ){ var reg=/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+ #]*[\w\-\@?^=%&/~\+#])?/; if (!reg.test(urlString)){ alert( "不是正確的網(wǎng)址吧,請(qǐng)注意檢查一下" ); } } } |
方法二:推薦
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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形式的URL- 199.194.52.184 + "|" // 允許IP和DOMAIN(域名) + "([0-9a-z_!~*'()-]+\.)*" // 域名- www. + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二級(jí)域名 + "[a-z]{2,6})" // first level domain- .com or .museum + "(:[0-9]{1,4})?" // 端口- :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.jfrwli.cn/article/1.htm"; alert(IsURL(testUrl)); |
剛發(fā)現(xiàn)一個(gè)不錯(cuò)的多功能測(cè)試函數(shù)的代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<script> /** * 正則表達(dá)式判斷網(wǎng)址是否有效 */ ( function (){ "use strict" ; var urlDict=[ //Bad Case 'www.baidu.com' , //常規(guī)網(wǎng)址,未帶協(xié)議頭的地址 'w.baidu.com' , //常規(guī)網(wǎng)址,短子域名 'baidu.com' , //常規(guī)網(wǎng)址,僅有主域名 '測(cè)試.com' , //非常規(guī)合法網(wǎng)址,中文域名不在參考之列 '1.2' , //錯(cuò)誤域名 ' WWWW ' , //無(wú)效字符串 '111測(cè)試' , //無(wú)效字符串 //Correct Case 'http://baidu.com' , //常規(guī)網(wǎng)址,僅有主域名 'http://www.baidu.com' , //常規(guī)網(wǎng)址,帶子域名 'https://www.baidu.com/' , //常規(guī)網(wǎng)址,使用https協(xié)議頭,帶根目錄 'http://www.baidu.com/api' , //常規(guī)網(wǎng)址,有一級(jí)目錄下資源 'http://www.subdomain.baidu.com/index/subdir' , //常規(guī)網(wǎng)址,多級(jí)子域名,多級(jí)目錄 'http://www.www.subdomain.baidu.com/index/subdir/' ,//常規(guī)網(wǎng)址,多級(jí)子域名,多級(jí)目錄,目錄地址閉合 'http://io.io' //非常規(guī)網(wǎng)址,多級(jí)子域名,多級(jí)目錄,目錄地址閉合 ]; // 建議的正則 function isURL(str){ return !!str.match(/(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*) #?(?:[\w]*))?)$/g); } // 不知道誰(shuí)寫的簡(jiǎn)單版的坑爹正則 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形式的URL- 199.194.52.184 + "|" // 允許IP和DOMAIN(域名) + "([0-9a-z_!~*'()-]+\.)*" // 域名- www. + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二級(jí)域名 + "[a-z]{2,6})" // first level domain- .com or .museum + "(:[0-9]{1,4})?" // 端口- :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 ); } } // 測(cè)試用例覆蓋 ( 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> |
運(yùn)行以后通過(guò)chorme的F12查看效果
上面介紹的主要是js函數(shù)的寫法與判斷方法,下面是小編整理的一些關(guān)于驗(yàn)證網(wǎng)址的正則表達(dá)式大家可以參考一下
正則表達(dá)式 |
(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?
|
---|---|
匹配 | http://regxlib.com/Default.aspx | http://electronics.cnet.com/electronics/0-6342366-8-8994967-1.html |
不匹配 | www.yahoo.com |
正則表達(dá)式 |
^\\{2}[\w-]+\\(([\w-][\w-\s]*[\w-]+[$$]?$)|([\w-][$$]?$))
|
---|---|
匹配 | \\server\service | \\server\my service | \\serv_001\service$ |
不匹配 | \\my server\service | \\server\ service | \\server$\service |
正則表達(dá)式 |
^(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\.\,\?\'\\/\+&%\$#\=~_\-@]*)*$
|
---|---|
匹配 | http://www.sysrage.net | https://64.81.85.161/site/file.php?cow=moo's |ftp://user:pass@host.com:123 |
不匹配 | sysrage.net |
正則表達(dá)式 |
^([a-zA-Z]\:|\\\\[^\/\\:*?"<>|]+\\[^\/\\:*?"<>|]+)(\\[^\/\\:*?"<>|]+)+(\.[^\/\\:*?"<>|]+)$
|
---|---|
匹配 | c:\Test.txt | \\server\shared\Test.txt | \\server\shared\Test.t |
不匹配 | c:\Test | \\server\shared | \\server\shared\Test.? |
正則表達(dá)式 |
^(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\.\,\?\'\\\+&%\$#\=~_\-]+))*$
|
---|---|
匹配 | http://site.com/dir/file.php?var=moo | https://localhost |ftp://user:pass@site.com:21/file/dir |
不匹配 | site.com | http://site.com/dir// |
正則表達(dá)式 |
^([a-zA-Z]\:)(\\[^\\/:*?<>"|]*(?<![ ]))*(\.[a-zA-Z]{2,6})$
|
---|---|
匹配 | C:\di___r\fi_sysle.txt | c:\dir\filename.txt |
不匹配 | c:\dir\file?name.txt |
正則表達(dá)式 |
^([a-zA-Z0-9]([a-zA-Z0-9\-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$
|
---|---|
匹配 | regexlib.com | this.is.a.museum | 3com.com |
不匹配 | notadomain-.com | helloworld.c | .oops.org |
正則表達(dá)式 |
^(((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\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$
|
---|---|
匹配 | www.blah.com:8103 | www.blah.com/blah.asp?sort=ASC |www.blah.com/blah.htm#blah |
不匹配 | www.state.ga | http://www.jb51.ru |
正則表達(dá)式 |
\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))
|
---|---|
匹配 | 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:password@example.com:8080 |http://userid@example.com | http://userid@example.com:8080 |http://userid:password@example.com |
不匹配 | no_ws.example.com | no_proto_or_ws.com | /relative_resource.php |