JavaScript get last item of string split on slash /

By.

min read

My profile

Share this:

[code:1:2936513c8b]var str = “var1/var2/var3”;

var rest = str.substring(0, str.lastIndexOf(“/”) + 1);
var last = str.substring(str.lastIndexOf(“/”) + 1, str.length);[/code:1:2936513c8b]

source:
http://jsfiddle.net/markcoleman/Haudj/1/

update:
[code:1:2936513c8b]str.split(’/’).pop() [/code:1:2936513c8b]

Source:
https://twitter.com/DannyvanKooten/status/668234111828406272

Share this:

Leave a Reply

Your email address will not be published. Required fields are marked *