用正则表达式解析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');

你可能感兴趣的:(正则表达式)