remove everything before the last occurrence of a character

Javascript

Javascript Problem Overview


I'm trying to perform the following action on a string :

  • find the last occurrence of the character "/";
  • remove everything before that character;
  • return the remains of the string;

To be more explicit, let's say I have the following string :

var string = "/Roland/index.php"; // Which is a result of window.location.pathname

Now what I need to extract out of it is everything but the actual page, something like this :

var result = "index.php" // Which is what I need to be returned

Of course, that is just an example, because obviously I will have different pages, but the same principles apply.

I was wondering if someone could help me out with a solution for it. I tried the next actions but with no success :

var location = window.location.pathname;
var result = location.substring(location.lastIndexOf["/"]);

Javascript Solutions


Solution 1 - Javascript

You have the right idea just replace the brackets with parentheses.

var string = "/Roland/index.php";
var result = string.substring(string.lastIndexOf("/") + 1);

Here is an example in jsfiddle and here is an explanation of the .lastIndexOf() method on the Mozilla Developer Network.

Solution 2 - Javascript

Personally I'd use a regular expression:

var result = string.replace(/^.*\/(.*)$/, "$1");

If you're familiar with regular expressions (and you should be if not :-) then it's not as alien-looking as it is when they're unfamiliar.

The leading ^ forces this regular expression to "anchor" the match at the start of the string. The \/ matches a single / character (the \ is to keep the / from confusing the regular expression parser). Then (.*)$ matches everything else from the / to the end of the string. The initial .* will swallow up as much as it can, including / characters before the last one. The replacement text, "$1", is a special form that means "the contents of the first matched group". This regex has a group, formed by the parentheses around the last .* (in (.*)$). That's going to be all the stuff after the last /, so the overall result is that the whole string is replaced by just that stuff. (If the pattern doesn't match because there aren't any / characters, nothing will happen.)

Solution 3 - Javascript

Split the string into an array on / and .pop() off the last element. Note, that you will first need to strip off a trailing slash if there is one.

var locationstring = window.location.pathname;
// replace() the trailing / with nothing, split on the remaining /, and pop off the last one
console.log(locationstring.replace(/\/$/, "").split('/').pop());

If in the case of a URL like /path/stuff/here/ where you have the trailing /, if that case should return an empty string rather than here, modify the above to remove the .replace() from the call chain. I assumed you would want the last component regardless of a trailing slash, but may have incorrectly assumed.

console.log(locationstring.split('/').pop());

Solution 4 - Javascript

    var result = /\/([^\/]*)$/.exec(location)[1];

//"remove-everything-before-the-last-occurrence-of-a-character#10767835"

Note: location here is the window.location, not your var location.

Solution 5 - Javascript

var string = "/Roland/index.php";
var result = string.substring(0, string.lastIndexOf("/") + 0);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRolandView Question on Stackoverflow
Solution 1 - JavascriptYevgeny SimkinView Answer on Stackoverflow
Solution 2 - JavascriptPointyView Answer on Stackoverflow
Solution 3 - JavascriptMichael BerkowskiView Answer on Stackoverflow
Solution 4 - JavascriptEsailijaView Answer on Stackoverflow
Solution 5 - JavascriptAshish GuptaView Answer on Stackoverflow