1、协议 如http,https@H_301_5@
2、域名或IP@H_301_5@
3、端口号,如7001,8080@H_301_5@
4、Web上下文@H_301_5@
5、URI,请求资源地址@H_301_5@
6、请求参数@H_301_5@
一个URL示例:@H_301_5@
http://i0.sinaimg.cn:8080/blog/register.jsp?type=a&name=test1234
@H_301_5@
@H_301_5@
这里只对前5个参数进行匹配解析:@H_301_5@
@H_301_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 context var tmp2 = tmp.substr(tmp.indexOf("/") + 1); var webContext = tmp2.substr(0,tmp2.indexOf('/')); //alert('webContext:' +webContext); //解析URI var 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.$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();原文链接:https://www.f2er.com/regex/361141.html