0

I want to get a specific part of a url between the third and fourth slashes of a link on the page.

EDIT: Sorry I don't think I was clear the first time, I meant getting the specific part of the url OF A LINK found on the page.

5
  • 2
    How can we help you with this? Have you tried to tackle this problem? Commented Jul 28, 2012 at 18:40
  • Are you sure? Bearing in mind that there are two consecutive slashes at the beginning of an absolute URL? Given the link 'http://www.example.com/directory1/directory2/directory3/directory4/index.html' what would you expect to get from 'between the third and fourth slashes'? Commented Jul 28, 2012 at 18:41
  • @Blender I already searched around but only found codes for getting specific parts of the url of the page you're on, not the link found on the page you're on. Commented Jul 28, 2012 at 18:42
  • you should elaborate you question and should specify which is your domain, that means on what purpose you are asking that question ?? Commented Jul 28, 2012 at 18:43
  • @David Thomas Ignoring the http:// slashes, I want to get the directory3 text Commented Jul 28, 2012 at 18:44

4 Answers 4

7
var getSegment = function (url, index) {
   return url.replace(/^https?:\/\//, '').split('/')[index];
}

Usage:

getSegment("http://domain.com/a/b/c/d/e", 4); // "d" 

The replace makes sure that the first two slashes after the protocol (http or https) don't count.

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

2 Comments

OP is asking for a part of the path of a url found on the page, not the current url.
Removed downvote. Note also though that if the url has a querystring or anchor, that will be included with the last segment.
2

Here's a working example of getting a particular path segment.

Code:

var url = "www.test.com/one/two/three?p1=v&p2=v#anc";
var file = url.split('?')[0];
var pathanddomain = file.split('/');
var path = pathanddomain.splice(1, pathanddomain.length-1);
var pathIndexToGet = 2;
document.write(path[pathIndexToGet]);​

If you want to do this for the current page, use:

var url = window.location.href;

Also, if your url starts with http(s)://, you will need to remove this.

5 Comments

The url will constantly change, is it possible I can change the first line to something like: var url = "www.test.com/one/two/*?p1=v&p2=v#anc";
@user1560022. You can change the first line to anything you like. It can be a variable, it can be hard coded (as the example), it can be pulled from the browser, or it can be produced by a function call.
I don't know what you're trying to achieve with the asterisk but if that's the url you have then yes, this code will work. I should warn you though, * is not a valid url character and you should avoid using it without it being url encoded.
Well as I said, the url will constantly change so hardcoding the url in this code will mean that I will have to change the code too. The asterisk means that anything could be there.
@user1560022. That's fine. It doesn't matter how deep the path is, the code will continue to work. The .split('/') returns an array of the path segments. You simply supply it with the index you require. .split('/')[2], .split('/')[25] etc.
0

I'd suggest:

var link = 'http://www.example.com/directory1/directory2/directory3/directory4/index.html';

console.log(link.split('/')[5]);​

JS Fiddle demo.

The reason we're using [5] not [4] is because of the two slashes at the beginning of the URL, and because JavaScript arrays are zero-based.

Comments

0

you should elaborate you question and should specify which is your domain, that means on what purpose you are asking that question ??

This may help you:

var urlValue = url.split("/");

Then store urlValue as array. then pick up third and forth value of the urlvalue on array.

1 Comment

Voting down because this needs more details or an example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.