I'm using custom URL paths which I want to map into an object.
My mapping looks like this:
browser://{A}/{B}/{C}/{D}/
with {C}
and {D}
being optional. So a path might look like this:
"browser://call/register/ss/hello"
"browser://call/request/"
I would like to have the path available as an object ala
elems = {
A: call,
B: register,
C: ss,
D: hello
}
But right now I can't even get the regex to work... Here is what I'm trying:
var re = /^browser:\/\/([\w\W]+)[\/+]([\w\W]+)[\/+]([\w\W]*)[\/*]([\w\W]*)[\/*]/;
console.log(re.exec("browser://call/register/ss/hello"));
This only seems to work if I include a full path. But when I leave away {C}
or {D}
, my regex fails.
Question
How do I make {C}
and {D}
optional? Is there an easy way to convert the resulting array into an object?
Thanks for help!