js正则捕获型分组与可选捕获型分组

js正则捕获型分组与可选捕获型分组:

()表示一个捕获型分组
(?:…..)表示一个非捕获型分组
(?:…..)?表示一个可选的非捕获型分组

var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var url = 'http://www.ora.com:80/goodparts?q#fragment';
var result = parse_url.exec(url);
var names = ['url','scheme','slash','host','port','path','query','hash'];
var blanks = '                  ';
var i;
for(i=0,l=names.length;i<l;i+=1){
    var str = names[i] + ':' + blanks.substring(names[i].length) + result[i];
    console.log(str);
}

var parse_url = /^     (?:([A-Za-z]+):)?      (\/{0,3})   ([0-9.\-A-Za-z]+)      (?::(\d+))?        (?:\/([^?#]*))?       (?:\?([^#]*))?      (?:#(.*))?       $/   ;
var parse_number = /^   -?   \d+   (?:\.\d*)?   (?:e[+\-]?\d+)?   $/i;