abstract:正則表達(dá)式(regular expression)描述了一種字符串匹配的模式,可以用來檢查一個(gè)串是否含有某種子串、將匹配的子串做替換或者從某個(gè)串中取出符合某個(gè)條件的子串等。則表達(dá)式:var match = /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&
正則表達(dá)式(regular expression)描述了一種字符串匹配的模式,可以用來檢查一個(gè)串是否含有某種子串、將匹配的子串做替換或者從某個(gè)串中取出符合某個(gè)條件的子串等。
則表達(dá)式:
var match = /^((ht|f)tps?):\/\/[\w\-]+(\.[\w\-]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?$/;
匹配:
(1)、直接匹配域名地址:
var matchString = 'https://i.cnblogs.com/'; console.log(match.test(matchString)); // ==> true
(2)、匹配鏈接含(*.htm,*.html,*.php,*.aspx...)后綴的地址:
var matchString = 'https://i.cnblogs.com/EditPosts.aspx'; console.log(match.test(matchString)); // ==> true
(3)、匹配含參數(shù)的地址:
var matchString = 'https://i.cnblogs.com/EditPosts.aspx?opt=1'; console.log(match.test(matchString)); // ==> true
使用說明:
(1)、地址必須以http/https/ftp/ftps開頭;
(2)、地址不能包含雙字節(jié)符號或非鏈接特殊字符。
更多關(guān)于匹配URL的正則表達(dá)式請關(guān)注PHP中文網(wǎng)(ipnx.cn)其它文章!