正则表达式解析URL

正则表达式:

var match = /^((ht|f)tps?:)\/\/([\w-]+(\.[\w-]+)*\/){1}(([\w-]+(\.[\w-]+)*\/?)*)?(\?([\w\-\.,@?^=%&:\/~\+#]*)+)?$/;
/* 
注:
(1)、如需允许其他联接方式,可以修改“(ht|f)tps?”部分,在“?”后面跟上符号“|”,然后加上您需要的联接方式,多个时用符号“|”分隔)。
(2)、如需允许URL参数包含其它字符,可以修改“[\w\-\.,@?^=%&:\/~\+#]”,以设置您需要的参数。
*/

匹配(说明):

var matchString = 'http://www.cnblogs.com/jschar/index.html?auther=jschar&date=2016-10-01';
console.log(match.exec(matchString));
/*
    [0]: "http://www.cnblogs.com/jschar/index.html?auther=jschar&date=2016-10-01"
    [1]: "http:" ==> window.document.location.protocol
    [2]: "ht"
    [3]: "www.cnblogs.com/" ==> window.document.location.host 或 window.document.location.hostname
    [4]: ".com" ==> 域名后缀名
    [5]: "jschar/index.html" ==> window.document.location.pathname
    [6]: "index.html" ==> 当前页面名称
    [7]: ".html" ==> 当前页面后缀名
    [8]: "?auther=jschar&date=2016-10-01" ==> window.document.location.search
    [9]: "auther=jschar&date=2016-10-01" ==> 当前页面的所有参数
*/

 使用说明:

(1)、地址必须以http/https/ftp/ftps开头;

(2)、地址不能包含双字节符号或非链接特殊字符。

 

posted on 2016-12-01 11:29  Kris゜  阅读(1560)  评论(0编辑  收藏  举报

导航