9

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.

6
  • So what's the question? What have you tried? The question shows little or no effort from you. Commented Sep 27, 2011 at 13:53
  • I tried googling it, and got nothing. Commented Sep 27, 2011 at 13:57
  • Have you tried writing it yourself? :) Commented Sep 27, 2011 at 14:01
  • Look again at indexOf() (hint: it has an optional second parameter). Commented Sep 27, 2011 at 14:04
  • Are you simply trying to the the pathname portion of a url string? Commented Sep 27, 2011 at 14:09

9 Answers 9

8

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.

Sign up to request clarification or add additional context in comments.

Comments

4
let s = 'http://something.com/somethingheretoo'; parts = s.split('/'); parts.splice(0, 2); return parts.join('/'); 

5 Comments

After third / does not mean "between third and fourth"
See my comment below, at stackoverflow.com/questions/7570276/….
Yes, I saw that before. That's why i changed my code. I delete the first elements of the string and join the end togther.
@PiTheNumber sorry to say that, but it seems like you haven't tested this piece of code.
@piotros yes, thanks! Still 3 upvotes!?! I fixed it now.
2

Try something like the following function, which will return the index of the nth occurrence of the search string s, or -1 if there are n-1 or fewer matches.

String.prototype.nthIndexOf = function(s, n) { var i = -1; while(n-- > 0 && -1 != (i = this.indexOf(s, i+1))); return i; } var str = "some string to test"; alert(str.nthIndexOf("t", 3)); // 15 alert(str.nthIndexOf("t", 7)); // -1 alert(str.nthIndexOf("z", 4)); // -1 var sub = str.substr(str.nthIndexOf("t",3)); // "test" 

Of course if you don't want to add the function to String.prototype you can have it as a stand-alone function by adding another parameter to pass in the string you want to search in.

Comments

1

If you want to stick to indexOf:

var string = "http://something/sth1/sth2/sth3/" var lastIndex = string.indexOf("/", lastIndex); lastIndex = string.indexOf("/", lastIndex); lastIndex = string.indexOf("/", lastIndex); string = string.substr(lastIndex); 

If you want to get the path of that given URL, you can also use a RE:

string = string.match(/\/\/[^\/]+\/(.+)?/)[1]; 

This RE searches for "//", accepts anything between "//" and the next "/", and returns an object. This object has several properties. propery [1] contains the substring after the third /.

Comments

1

Another approach is to use the Javascript "split" function:

var strWord = "me/you/something"; var splittedWord = strWord.split("/"); 

splittedWord[0] would return "me"

splittedWord[1] would return "you"

splittedWord[2] would return "something"

1 Comment

var strWord = "me/you/something/haha". splittedWord[3] = "something", while the OP wants to match "something/haha".
1

It sounds like you want the pathname. If you're in a browser, keep an a element handy...

var _a = document.createElement('a'); 

...and let it do the parsing for you.

_a.href = "http://something.com/somethingheretoo"; alert( _a.pathname.slice(1) ); // somethingheretoo 

DEMO: http://jsfiddle.net/2qT9c/

Comments

0

In your case, you could use the lastIndexOf() method to get the 3rd forward slash.

2 Comments

The last is not the same as the third, even if it seems so in the example.
Very true, but for OP's question, I made the assumption he wanted the last bit of the path and used the weasel phrase "In your case" :)
0

Here's a very cool way of handling this: How can I remove all characters up to and including the 3rd slash in a string?

My preference of the proposed solutions is

var url = "http://blablab/test/page.php"; alert(url.split("/")[3]); //-> "test" 

Comments

0

Inestead of using indexOf it is possible to do this this way:

const url = 'http://something.com/somethingheretoo'; const content = new URL(url).pathname.slice(1); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.