下面这段代码来自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*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};

解决方案 »

  1.   


    想获得一个什么样的值呢? 这个本身就是分析一个URI得到各个部分的值
      

  2.   

    修改参考返回一个字符串... :)<html>
    <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>
      

  3.   

    我想得到是下面这样的,这几个是我看了几天正则表达式重写出来的:
    获得协议:^((\w+):\/\/)
    获得用户信息:((\w+):?(\w+)?@)
    获得主机:(([\w]+\.)+\w+)
    获得请求参数:\?([^\?^#]+)?
    获得锚点:\?([^\?^#]+)?没有实现的是面的,求正则表达式高手帮忙:
    获得用户名:
    获得密码:
    获得端口:
    获得relative:
    获得路径:
    获得目录:
    获得文件:
      

  4.   


    <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>