下面这段代码来自http://blog.stevenlevithan.com/archives/parseuri
可以把一个URL分解为十多个值,正则表达式实在搞不懂,好几天了,也没分解开,
那位高手可以帮我分解成一个表达式获得一个值的写法吗,先行谢过
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT Licensefunction parseUri (str) {
var o = parseUri.options,
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14; while (i--) uri[o.key[i]] = m[i] || ""; uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
}); return uri;
};parseUri.options = {
strictMode: false,
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
可以把一个URL分解为十多个值,正则表达式实在搞不懂,好几天了,也没分解开,
那位高手可以帮我分解成一个表达式获得一个值的写法吗,先行谢过
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT Licensefunction parseUri (str) {
var o = parseUri.options,
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14; while (i--) uri[o.key[i]] = m[i] || ""; uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
}); return uri;
};parseUri.options = {
strictMode: false,
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
解决方案 »
- JS问题,帮忙瞅瞅
- JavaScript问题,请高手指教
- JS 点击小图片出现大图片
- extjs 树 伸展 空白
- 关于控件,如果id不是按顺序的排,为什么就实现不了了呢
- DIV设置overflow:auto 后怎样用javascript 滚动到想要看的区域?
- IE中不显示xml数据
- js时间控件问题求助!!!
- 请问如何window.close的同时避免弹出是否关闭的对话框(不升级到ie6)
- 怎样用脚本实现:根据屏幕的分辨率设置<object width=xx height=yy></object>中的宽和高?
- JS代码斜杠转义的问题,试了好久没出来。急,高手帮下忙。谢谢
- extjs 时间格式转换
想获得一个什么样的值呢? 这个本身就是分析一个URI得到各个部分的值
<head>
<title>javascript</title>
<meta http-equiv="content-type" content="text/html";charset=utf-8>
<script type="text/javascript">
<!--
// parseUri 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT Licensefunction parseUri (str) {
var o = parseUri.options,
m = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
uri = {},
i = 14; while (i--) uri[o.key[i]] = m[i] || ""; uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
}); //return uri;
//alert(o.key[2]);
//alert(uri[o.key[2]]);
var xitem,reStr="";
for(i=0;i<o.key.length;i++){
xitem=o.key[i];
reStr+="Name : " + xitem + " value = "+uri[xitem]+"\n";
}
return reStr;
};
parseUri.options = {
strictMode: false,
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
function $(id){return typeof(id=="string") ? document.getElementById(id) : id}
var strURL="http://topic.csdn.net/u/20100830/16/c78e8e5c-0427-4acd-9d8b-8f5145a2b742.html?seed=550163432&r=68036422#r_68036422";
var x=parseUri(strURL);
alert(x);
-->
</script>
</head>
<body><br>
<br>
http://topic.csdn.net/u/20100830/16/c78e8e5c-0427-4acd-9d8b-8f5145a2b742.html?seed=550163432&r=68036422#r_68036422
</body>
</html>
获得协议:^((\w+):\/\/)
获得用户信息:((\w+):?(\w+)?@)
获得主机:(([\w]+\.)+\w+)
获得请求参数:\?([^\?^#]+)?
获得锚点:\?([^\?^#]+)?没有实现的是面的,求正则表达式高手帮忙:
获得用户名:
获得密码:
获得端口:
获得relative:
获得路径:
获得目录:
获得文件:
<script type="text/javascript">
function parseURL(url) {
var a = document.createElement('a');
a.href = url;
return {
source: url,
protocol: a.protocol.replace(':',''),
host: a.hostname,
port: a.port,
query: a.search,
params: (function(){
var ret = {},
seg = a.search.replace(/^\?/,'').split('&'),
len = seg.length, i = 0, s;
for (;i<len;i++) {
if (!seg[i]) { continue; }
s = seg[i].split('=');
ret[s[0]] = s[1];
}
return ret;
})(),
file: (a.pathname.match(/\/([^\/?#]+)$/i) || [,''])[1],
hash: a.hash.replace('#',''),
path: a.pathname.replace(/^([^\/])/,'/$1'),
relative: (a.href.match(/tps?:\/\/[^\/]+(.+)/) || [,''])[1],
segments: a.pathname.replace(/^\//,'').split('/')
};
}
var myURL = parseURL('http://abc.com:8080/dir/index.html?id=255&m=hello#top');
myURL.file; // = 'index.html'
myURL.hash; // = 'top'
myURL.host; // = 'abc.com'
myURL.query; // = '?id=255&m=hello'
myURL.params; // = Object = { id: 255, m: hello }
myURL.path; // = '/dir/index.html'
myURL.segments; // = Array = ['dir', 'index.html']
myURL.port; // = '8080'
myURL.protocol; // = 'http'
myURL.source; // = 'http://abc.com:8080/dir/index.html?id=255&m=hello#top'</script>