Totally new to programming so kindly excuse the silly question.
I have this URL http://www.blahblah.com/some/thing/blah..
Now i need the string that appears after the third”/” ie., just “/some/thing”..
I dont want to go from reverse as the URL is different for each page but the protocol and the host name is the same..
Is there a way i can extract “/some/thing” along using Javascript..
Appreciate any help..
Thanks!!
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:
Alternative #1, to reduce repeat:
Alternative #2, if input can contain newlines:
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):Note that the last
\/has a?after it so it will return “” for “http://foo” and theiis so it will matchHTTP://if that is used for some strange reason (completely optional, of course).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.