I want to join two strings each representing a relative URL in Javascript.
I want to join the base URL http://www.adress.com/more/evenmore
with the following examples:
../../adress
(with the expected output: http://www.adress.com/adress
)../adress
(with the expected output http://www.adress.com/more/adress
)What would be the best way? I was thinking of using regexp and checking
how many ../
preceed the relative URL, then subtracting that amount from the baseurl and adding them to what is left.
8 years later, many browsers (except for Internet Explorer) support the URL constructor (URL(url [, base])
).
> new URL('../address', 'http://www.adress.com/more/evenmore/').href "http://www.adress.com/more/address" > new URL('../../address', 'http://www.adress.com/more/evenmore/').href "http://www.adress.com/address"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With