0

I have the string

var str = "A > B > C > D > E";

expected output:

E

Means i want to remove/replace all the string upto last > symbol, or before > symbol from last.

Javascript replace will do the trick, but i don't know how to write the pattern for this.

or can we do this by split method?

3 Answers 3

4

Using regex :

var str = "A > B > C > D > E";
var re = str.replace(/.*> /,"");

# print "E"
Sign up to request clarification or add additional context in comments.

1 Comment

ah it's worked, i was trying this from last 1 hour. thank u so much.
2

Goran's solution is correct and beautiful (because of the regex). However, here's an alternative using String's .split method:

var str = "A > B > C > D > E";
var letters = str.split(' > '); 
var output = letters[letters.length - 1];

Comments

1

You can do it even simpler than using split or replace. Locate the last separator, and take the part of the string after that:

str = str.substr(str.lastIndexOf(' > ') + 3);

Demo: http://jsfiddle.net/Guffa/ahgB2/

1 Comment

nice solution. new approach.

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.