Is there a known JavaScript regular expression to match an entire URL Connection String?
protocol://user:password@hostname:12345/segment1/segment2?p1=val1&p2=val2
I'm looking for a single regular expression that would help me translate such a connection string into an object:
{
protocol: 'protocol',
user: 'user',
password: 'password',
host: 'hostname:12345',
hostname: 'hostname',
port: 12345,
segments: ['segment1', 'segment2'],
params: {
p1: 'val1',
p2: 'val2'
}
}
Also, I want every single part of the connection string to be optional, so the missing parameters can be filled by values from the environment.
examples:
protocol://
server:12345
:12345
- for the port onlyuser:password@
user@
:password@
/segment1
?p1=val1
- and so on...
Standard RFC 3986 rules should apply to all the parts when it comes to the valid symbols.
I'm looking for something that would work in both Node.js and all browsers.
I've done a separate parsing piece-by-piece within connection-string, but the problem with that - it doesn't allow to validate, i.e. to tell if the whole thing is valid.