Reputation: 390
I have a variable that returns urls like this: https://abc.example.com/bla/bla/123/bla
where abc1.example
it's not the same for every case (also can be http
instead of https
).
I'd like it to return only /bla/bla/123/bla
and for that I tried:
renamedObj.context_page_path = renamedObj.context_page_url.replace(new RegExp(renamedObj.context_page_url, 'g'), '').replace('https://', '').replace('http://', '');
But only gives me an empty string: context_page_path = ''
Upvotes: 0
Views: 93
Reputation: 34556
You need to replace everything from the start and up to the first slash that is not part of the protocol.
renamedObj.context_page_url.replace(/^https?:\/\/[^\/]+/, '');
Your current attempt is returning an empty string as you're basically telling it to replace the entirety with nothing. It equates to:
'foo'.replace('foo', ''); //""
[EDIT - REGEX pattern explanation]
OK so let's look at /^https?:\/\/[^\/]+/
First of all, the opening and closing /
are delimiters. They tell JavaScript that what's contained inbetween should be interpreted as as REGEX pattern, not a string.
There's two ways of defining REGEX patterns - REGEX literals, as in this case, and dynamic patterns via new RegExp(string)
. See my other answer on the difference between the two.
And so to the pattern itself:
^
tells JS the match must begin from the start of the string; it's not acceptable for the match to merely appear somewhere in the string; it must be from the beginninghttps?:\/\/
matches the protocol. The ?
after s
says the s
is optional, since you say it could be http
or https
. The forward slashes need to be 'escaped' (with blackslashes) so they don't conflict with our delimiters which, as discussed, are also forward slashes.[^\/]+
says "from that point on, match everything that isn't a forward slash" (which again has to be escaped.) So it will keep going right up to the slash that precedes the first "bla".So all in all, we're removing everything before the pathname part of the URL.
Upvotes: 2