正則表達式匹配URL

正則表達式:php

複製代碼
var match = /^((ht|f)tps?):\/\/([\w\-]+(\.[\w\-]+)*\/)*[\w\-]+(\.[\w\-]+)*\/?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?/;
/* 
注:
(1)、如需容許其餘聯接方式,能夠修改「(ht|f)tps?」部分,在「?」後面跟上符號「|」,而後加上您須要的聯接方式,多個時用符號「|」分隔)。
(2)、如需容許URL參數包含其它字符,能夠修改「[\w\-\.,@?^=%&:\/~\+#]」,以設置您須要的參數。
*/
複製代碼

匹配:html

(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...)後綴的地址:post

複製代碼
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)、匹配含參數的地址:spa

複製代碼
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
複製代碼

使用說明:code

(1)、地址必須以http/https/ftp/ftps開頭;htm

(2)、地址不能包含雙字節符號或非連接特殊字符。blog

 

完美,只是由於簡單。
 
分類:  js, reg
轉載:http://www.cnblogs.com/jschar/p/6092585.html
相關文章
相關標籤/搜索