溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

JS驗證URL函數(shù)的正則有哪些

發(fā)布時間:2021-10-11 10:41:19 來源:億速云 閱讀:134 作者:小新 欄目:互聯(lián)網(wǎng)科技

這篇文章給大家分享的是有關(guān)JS驗證URL函數(shù)的正則有哪些的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

這個url的正則表達(dá)式判斷的JavaScript!比較全面的。它驗證的情況包括IP,域名(domain),ftp,二級域名,域名中的文件,域名加上端口!用戶名等等信息,貌似作者也是在網(wǎng)上找的,我從一個項目代碼中扣出來的,是我見過的最強(qiáng)最全面的url驗證方式!太猛了,貼在這里與大家分享,URL的驗證實在是很頻繁。

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].' // 二級域名 
+ '[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); 
} 
}

代碼二:

function CheckUrl(str) { 
var RegUrl = new RegExp(); 
RegUrl.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");//jihua.cnblogs.com 
if (!RegUrl.test(str)) { 
return false; 
} 
return true; 
}

代碼三:

function checkUrl(urlString){
if(urlString!=""){
var reg=/(http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?/;
if(!reg.test(urlString)){
alert("不是正確的網(wǎng)址吧,請注意檢查一下");
						}
					}
}

下面給大家分享一個常用的驗證網(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\.&amp;%\$\-]+)*@)*((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\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$

匹配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\.\,\;\?\'\\\+&amp;%\$#\=~_\-]+))*$

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

感謝各位的閱讀!關(guān)于“JS驗證URL函數(shù)的正則有哪些”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI