9

I have URLs in the form:

serverName/app/image/thumbnail/2012/4/23/1335228884300/bb65efd50ade4b3591dcf7f4c693042b

Where serverName is the domain name of the server.

I would like to write a JS function that accepts one of these URLs and returns the very last (right-most) forward-slash-delimited string. So if the URL above was passed into the function, it would return "bb65efd50ade4b3591dcf7f4c693042b";.

function getImageDirectoryByFullURL(url) {
    // ... Not sure how to define regexp to delimit on forward slashes,
    // or how to start searching from end of string.
}

5 Answers 5

16

split by slashes /, pop off the last and return it

function getImageDirectoryByFullURL(url){
    return url.split('/').pop()
}

//a step by step breakdown
function getImageDirectoryByFullURL(url){
    url = url.split('/'); //url = ["serverName","app",...,"bb65efd50ade4b3591dcf7f4c693042b"]
    url = url.pop();      //url = "bb65efd50ade4b3591dcf7f4c693042b"
    return url;           //return "bb65efd50ade4b3591dcf7f4c693042b"
}

what this does is split the url per / and returns an array of values in between, but not including, the slashes. then, since what's returned by split() is an array, we can use pop() to pop off the last item and return it.

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

3 Comments

Good example of doing OP wants, not what he thinks he wants :P
How would I pop off that last index in the returned array?
@AdamTannon split() returns an array which pop() can use. pop() returns the last item in the array provided by split(), and thus is what's returned by the whole function.
6

In this case, substr() might be faster than split(). Not 100% sure.

function getImageDirectoryByFullURL(url){
    return url.substr(url.lastIndexOf("/")+1);
}

Edit: I forgot, you don't need to include the extra length parameter. Without passing that in you just get a substr to the end of the string, which is what is desired. While this solution is admittedly a little uglier than Joseph's answer, it is twice as fast in Chrome and something like fives times as fast in Firefox.

5 Comments

I doubt the speed difference is large enough to matter. Your solution and the one proposed by Joseph are both valid (though I personally prefer his because it's shorter and more succinct).
Yep. Just thought I'd propose a different method of doing it, because... well, just because. =]
+1, great alternative, just add a +1 here lastIndexOf("/")+1 to remove the first /
amazingly, this is way much faster that the pop solution jsperf.com/popvslastindex
@ajax333221: Wow, thanks for the test. I suspected it would be faster; just didn't know how much faster. I'm betting it's because with the split() solution you have the overhead of creating the array and then calling the pop() function on it, whereas str.substr() just immediately returns the requested string.
3

To make it a little more robust and allow for the possible presence of a trailing slash, hash tags or query parameters on the URL:

function getImageDirectoryByFullURL(url){
    url = url.replace(/#[^#]+$/, "").replace(/\?[^\?]+$/, "").replace(/\/$/, "");
    return url.substr(url.lastIndexOf("/") + 1);
}

And a working demo with a bunch of test cases: http://jsfiddle.net/jfriend00/akVVf/

2 Comments

@ElliotBonneville— +1 for the ridiculous +1. ;-)
I'm not sure why you think the regexes are ridiculous. /#[^#]+$/ matches from the last # to the end of the string. Same for /\?[^\?]+$/ with a ?.
1

Since there are no sensible regular expression versions, consider:

return url.replace(/^.*\//,'');

2 Comments

Better hope the URL doesn't have a trailing slash on it.
The OP can sort that, the requirement was "…the very last (right-most) forward-slash-delimited string".
0

Also you can try something like this, getting the same result.

function getImageUrl( url){
    var result = url.substring(url.lastIndexOf("/") + 1);
    return result;
}

1 Comment

See my answer; also keep in mind that Javascript functions are case-sensitive (LastIndexOf should be lastIndexOf), and you'll want lastIndexOf("/") + 1 to avoid including the extra "/" at the beginning of the string.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.