I have a variable var1/var2/var3
. I want to store var3
the part after last slash in a variable and the part before that (var1/var2/
) in another variable. How can I do this?
You can use lastIndexOf
to get the last variable and that to get the rest.
var str = "var1/var2/var3";
var rest = str.substring(0, str.lastIndexOf("/") + 1);
var last = str.substring(str.lastIndexOf("/") + 1, str.length);
console.log(rest);
console.log(last);