標籤:blog pre 地址 正則表達 ftp code log 地方 str
Regex:
var match = /^((ht|f)tps?):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?/;
/*
註:
(1)、如需允許其他聯結方式,可以修改“(ht|f)tps?”部分,在“?”後面跟上符號“|”,然後加上您需要的聯結方式,多個時用符號“|”分隔)。
(2)、如需允許URL參數包含其它字元,可以修改“[\w\-\.,@?^=%&:\/~\+#]”,以設定您需要的參數。
*/
匹配:
(1)、直接匹配網域名稱地址:
var matchString = ‘https://i.cnblogs.com‘;console.log(match.test(matchString)); // ==> true
var matchString = ‘https://i.cnblogs.com/‘;console.log(match.test(matchString)); // ==> true
var matchString = ‘https://i.cnblogs.com//‘; // ==> 不允許非網域名稱或參數以外的地方出現雙“/”;console.log(match.test(matchString)); // ==> false
(2)、匹配連結含(*.htm,*.html,*.php,*.aspx...)尾碼的地址:
var matchString = ‘https://i.cnblogs.com/EditPosts.aspx‘;console.log(match.test(matchString)); // ==> true
var matchString = ‘https://i.cnblogs.com./EditPosts.aspx‘; // ==> 不允許參數以外的地方以雙“.”結尾;console.log(match.test(matchString)); // ==> false
(3)、匹配含參數的地址:
var matchString = ‘https://i.cnblogs.com/EditPosts.aspx?opt=1‘;console.log(match.test(matchString)); // ==> true
var matchString = ‘https://i.cnblogs.com/EditPosts.aspx?opt=1&user=‘console.log(match.test(matchString)); // ==> true
使用說明:
(1)、地址必須以http/https/ftp/ftps開頭;
(2)、地址不能包含雙位元組符號或非連結特殊字元。
Regex匹配URL