Если вы хотите увидеть хорошо протестированную библиотеку для извлечения частей URI, я бы ознакомился с методами goog.uri.utils библиотеки Google Closure.
https://github.com/google/closure-library/blob/8e44fb343fff467938f9476ba7f727c6acac76d8/closure/goog/uri/utils.js#L187
Вот регулярное выражение, которое делает тяжелую работу:
goog.uri.utils.splitRe_ = new RegExp(
'^' +
'(?:' +
'([^:/?#.]+)' + // scheme - ignore special characters
// used by other URL parts such as :,
// ?, /, #, and .
':)?' +
'(?://' +
'(?:([^/?#]*)@)?' + // userInfo
'([\\w\\d\\-\\u0100-\\uffff.%]*)' + // domain - restrict to letters,
// digits, dashes, dots, percent
// escapes, and unicode characters.
'(?::([0-9]+))?' + // port
')?' +
'([^?#]+)?' + // path
'(?:\\?([^#]*))?' + // query
'(?:#(.*))?' + // fragment
'$');