Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove everything before the last occurrence of a character

Tags:

javascript

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["/"]); 
like image 858
Roland Avatar asked May 26 '12 16:05

Roland


People also ask

How do you delete everything after a certain character?

Press Ctrl + H to open the Find and Replace dialog. In the Find what box, enter one of the following combinations: To eliminate text before a given character, type the character preceded by an asterisk (*char). To remove text after a certain character, type the character followed by an asterisk (char*).

How do I remove all characters from a string before a specific character?

Remove everything before a character in a string using Regex We need to use the “. *-” as a regex pattern and an empty string as the replacement string. It deleted everything before the character '-' from the string.

How do you remove everything from a string after a character?

Use the String. slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); .

How do I delete everything before a character in python?

To remove everything before a character in a string: Use the str. find() method to get the index of the character. Use string slicing and set the start index to the index of the character.


2 Answers

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.

like image 152
Yevgeny Simkin Avatar answered Sep 20 '22 20:09

Yevgeny Simkin


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.)

like image 40
Pointy Avatar answered Sep 22 '22 20:09

Pointy