You can probably simplify this regular expression. Try this to test if it does the same as yours:

/^(?:([A-z]+):)?\/\/([\w.-]+)(?::(\d{0,5}))?([\w._\/-]*)?(?:\?([\w_-]*))?(?:#(.*))?$/;