JavaScript indexOf() - how to get specific index

Itai Sagi picture Itai Sagi · Sep 27, 2011 · Viewed 10.7k times · Source

Let's say I have a URL:

http://something.com/somethingheretoo

and I want to get what's after the 3rd instance of /?

something like the equivalent of indexOf() which lets me input which instance of the backslash I want.

Answer

Kelly picture Kelly · Sep 27, 2011

If you know it starts with http:// or https://, just skip past that part with this one-liner:

var content = aURL.substring(aURL.indexOf('/', 8));

This gives you more flexibility if there are multiple slashes in that segment you want.