I've always been bad using regular expressions, but I think now I have a legitimate need, and I'm not sure if it's possible to accomplish it using them.
I want a regex that, when executed, returns all components of a given URL. It's not for validating format: precondition is that a correct URL is passed (usually it will be location.href).
Desired components are:
- scheme
- domain
- port (optional)
- path
- query string
Bonus:
- query string arguments separately
- fragment
- user / password
Examples:
/regex/.exec('http://www.stackoverflow.com/questions/1/regex-for-getting-url-components-in-javascript') --> ["http://stackoverflow.com/questions/30868359/regex-for-getting-url-components-in-javascript", "http", "www.stackoverflow.com", undefined, "questions/30868359/regex-for-getting-url-components-in-javascript"]
/regex/.exec('https://localhost:8080/?a=1&b=2') --> ["http://www.stackoverflow.com/questions/1/regex-for-getting-url-components-in-javascript", "https", "localhost", "8080", "", "a=1&b=2"]
EDIT:
In order to clarify, what I need is a small code which creates an object which represents a URl. Then I must be able to modify components such as parameters, schema, etc, and get the result again as a string. AFAIK, I can't do this with native location object, but I must be wrong.
The size of the code must be specially small, as this must be loaded synchronously in the header of the page. It's possible that it will be finally copied in every page instead of included as an external file. So, at first, I prefer to not rely on external dependencies.