用js正则表达式解析URL链接提取Host,Protocol等信息

要求:用javascript正则表达式解析URL链接,并从中提出关键信息

正则表达式:

/^(?:(\w+):\/\/)?(?:(\w+):?(\w+)?@)?([^:\/\?#]+)(?::(\d+))?(\/[^\?#]+)?(?:\?([^#]+))?(?:#(\w+))?/

测试用URL链接:http://username:password@www.bbs0101.com:1234/artical/js/index.php?key1=js&key2=css#anchor

测试代码:

var pattern = /^(?:(\w+):\/\/)?(?:(\w+):?(\w+)?@)?([^:\/\?#]+)(?::(\d+))?(\/[^\?#]+)?(?:\?([^#]+))?(?:#(\w+))?/;
var url = 'http://username:password@www.bbs0101.com:1234/artical/js/index.php?key1=js&key2=css#anchor';
var result = pattern.exec(url);

result返回值:

[
    "http://username:password@www.bbs0101.com:1234/artical/js/index.php?key1=js&key2=css#anchor", 
    "http", 
    "username", 
    "password", 
    "www.bbs0101.com", 
    "1234", 
    "/artical/js/index.php", 
    "key1=js&key2=css", 
    "anchor"
]