標籤:track data .com 索引 contex 資源 使用 r.js 網域名稱
對URL進行解析,一般用到的參數有:
1、協議 如http,https
2、網域名稱或IP
3、port號,如7001,8080
4、Web上下文
5、URI。請求資源地址
6、請求參數
一個URL示範範例:
http://i0.sinaimg.cn:8080/blog/register.jsp?
type=a&name=test1234
這裡僅僅對前5個參數進行匹配解析:
//使用字元索引對URL進行解析function parseURL(url){//解析協議var protocal = url.substring(0, url.indexOf(':'));//alert('protocal:' + protocal);//解析網域名稱和連接埠var tmp = url.substr(url.indexOf('//') + 2);var domain = tmp.substr(0, tmp.indexOf("/"));//alert('domain:' + domain);var domainName,port;var idx = domain.indexOf(":");if(idx>0){domainName = domain.substr(0,idx);port = domain.substr(idx + 1);} else{domainName = domain;}//alert('domainName:' + domainName + ',' + port);//解析web contextvar tmp2 = tmp.substr(tmp.indexOf("/") + 1);var webContext = tmp2.substr(0,tmp2.indexOf('/'));//alert('webContext:' +webContext);//解析URIvar uri = tmp2.substr(tmp2.indexOf('/'));//alert('uri:' + uri);return {protocal: protocal,domainName:domainName,port: port,webContext: webContext,uri: uri}}function testParseUrl(){var url = "http://ppp.com:8090/mximprove/mxt/scripts/views/MainViewController.js";var urlObj = parseURL(url);document.writeln('<br>' + urlObj.protocal);document.writeln('<br>' + urlObj.domainName);document.writeln('<br>' + urlObj.port);document.writeln('<br>' + urlObj.webContext);document.writeln('<br>' + urlObj.uri);}//testParseUrl();//----------------------------------------------------------------------------------------------------------------//使用正則表達式對URL進行解析function parseUrl(url){var reg = /^(\w+):\/\/([^\/:]*)(?::(\d+))?\/(.*)/reg.exec(url);alert(RegExp.$1 + ',' + RegExp.$2 + ',' + RegExp.$3 + ',' + RegExp.$4+ ',' + RegExp.$5);}function parseUrl2(url){var reg = /^(\w+):\/\/([^\/:]*)(?
::(\d+))?\/([^\/]*)(\/.*)/reg.exec(url);alert(RegExp.$1 + ',' + RegExp.$2 + ',' + RegExp.$3 + ',' + RegExp.$4+ ',' + RegExp.$5);}function testParseUrl(){var url = "http://ppp.com:8090/mximprove/mxt/scripts/views/MainViewController.js";parseUrl2(url);var url2 = "http://ppp.com/mximprove/mxt/scripts/views/MainViewController.js";parseUrl2(url2);}//testParseUrl();
使用正則表達式對URL進行解析