用正则表达式解析url

function parseUrl(url) {
    var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
    var result = parse_url.exec(url);
    var names = ['url', 'scheme', 'slash', 'host', 'port', 'path', 'query', 'hash'];
    var blanks = '            ';
    var i;
    for (var i = 0, len = names.length; i < len; i += 1) {
        document.write(names[i] + ':' + blanks.substring(names[i].length), result[i] + '<br/>');
    }
}

parseUrl('http://zw.zftec.gov.cn/home/details/tzggDetails.htm?id=746212&catalogid=7&page_encoding=UTF-8');
posted @ 2012-07-16 14:19  小猩猩君  阅读(3074)  评论(0编辑  收藏  举报