I would use a simple regular expression here.
One way to get stuff after the third "/" just throw it away with everything before it.
Basic idea:
"/a/b/c/d/e/f".replace(/.*?\/.*?\/s.*?\//, "")
// -> "c/d/e/f"
Alternative #1, to reduce repeat:
"/a/b/c/d/e/f".replace(/(?:.*?\/){3}/, "")
Alternative #2, if input can contain newlines:
"/a/b/c/d/e/f".replace(/(?:[^\/]*\/){3}/, "")
The *?
qualifier means "don't be greedy" so .
won't skip over /
's. It's not needed in the negative character class case because [^\/]
will never match /
.
This could of course be written in a "less generic" form because because the real intent is just to git rid of the protocol/domain portions of the URI (the number of /
's is only an observed relationship):
"http://foo/a/b/c/d/e/f".replace(/https?:\/\/[^\/]*\/?/i, "")
// -> "a/b/c/d/e/f"
Note that the last \/
has a ?
after it so it will return "" for "http://foo" and the i
is so it will match HTTP://
if that is used for some strange reason (completely optional, of course).
"HTTPS://foo".replace(/https?:\/\/[^\/]*\/?/i, "")
// -> ""
Happy coding!
Note: all of the above regular expressions will extract the content after the 3rd /
, as stated in the question. They will need to be slightly modified to include the 3rd /
in the result. Also, it helps to not ask X-Y questions.