Split variable from last slash
JavascriptJavascript Problem Overview
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?
Javascript Solutions
Solution 1 - Javascript
You can use lastIndexOf
to get the last variable and that to get the rest.
var rest = str.substring(0, str.lastIndexOf("/") + 1);
var last = str.substring(str.lastIndexOf("/") + 1, str.length);
Example on jsfiddle.
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);
Solution 2 - Javascript
Try something like this:
var vars = "var1/var2/var3";
var arrVars = vars.split("/");
var lastVar = arrVars.pop();
var restVar = arrVars.join("/");
alert(lastVar);
alert(restVar);
Solution 3 - Javascript
var last = url.split("/").pop();
console.log(last);
You can get last piece of url with array pop method.
Solution 4 - Javascript
var txt = "var1/var2/var3";
txt = txt.split('/')
var Var1 = txt.pop();
var Var2 = txt[0]+'/'+txt[1];
alert(Var1);
alert(Var2);
Solution 5 - Javascript
You can use a split and then pull the last index of it, like so :
Example:
var string = 'var1/var2/var3';
var result = string.split('/'); //Splits into an array
//var final = result[result.length -1]; //Grabs last value
//result.pop(); //Removes last value
var final = result.pop(); //Removes last value and grap the last value
var previous = result.join('/'); //Grabs the previous part
alert("Previous: " + previous + ", Final Part: " + final); //Alerts results
Demo:
Solution 6 - Javascript
Use this function:
function splitLast(s, sep = ' ') {
/* Split `s` on the last occurrence of `sep` and return both parts
as an array, [left,right]; or return ["",s] if no occurrence was found.
*/
let right = s.split(sep).pop()
let left = s.substring(0, s.length - right.length - sep.length)
return [left, right]
}
Call it like:
let [left, right] = splitLast('var1/var2/var3', '/')
// left: "var1/var2"
// right: "var3"
Solution 7 - Javascript
string.substring(start,end)
where
start = Required. The position where to start the extraction. First character is at index 0
end = Optional. The position (up to, but not including) where to end the extraction. If omitted, it extracts the rest of the string
var string = "var1/var2/var3";
start = string.lastIndexOf('/'); //console.log(start); o/p:- 9
end = string.length; //console.log(end); o/p:- 14
var string_before_last_slash = string.substring(0, start);
console.log(string_before_last_slash);//o/p:- var1/var2
var string_after_last_slash = string.substring(start+1, end);
console.log(string_after_last_slash);//o/p:- var3
OR
var string_after_last_slash = string.substring(start+1);
console.log(string_after_last_slash);//o/p:- var3
Solution 8 - Javascript
Solution using regex, might not be the fastest, but takes less space and might be more readable.
"var1/var2/var3".split(/\/(?=[^\/]+$)/)
\/
– Match a slash(?=
– If it's followed by[^\/]+
– Anything but slashes.$
– And the end of the string
)
Solution 9 - Javascript
The simplest solution is to use javascript:
var str = "var1/var2/var3/var4/var5";
var splitted = str.split("/");
var first = "";
for (var i=0; i<splitted.length-1; i++) {
first += splitted[i] + "/";
}
var second = "";
if (splitted.length > 0) {
second = splitted[splitted.length-1];
}
alert(first); // var1/var2/var3/var4/
alert(second); // var5
edited:
but the shortest solution will be: substring()
use
Solution 10 - Javascript
You can also repeatedly do replace()
, split()
, and join()
on the string to get the desired result
let str = 'var1/var2/var3';
str = str.split('').reverse().join('').split(/\/(.+)/).map(x => x.split('').reverse().join('')).reverse().filter(x => x);
console.log(str);
Explanation:
str
.split('') /* ['v', 'a', 'r', '/', 'v', 'a', ...] */
.reverse() /* ['3', 'r', 'a', 'v', '/', 'r', ...] */
.join('') /* '3rav/2rav/1rav' */
.split(/\/(.+)/) /* ['3rav', '2rav/1rav', ''] */
.map(x => x.split('').reverse().join('')) /* ['var3', 'var1/var2', ''] */
.reverse() /* ['', 'var1/var2', 'var3'] */
.filter(x => x) /* ['var/var2', 'var3'] */
Solution 11 - Javascript
With ES20, and new time coding, it is much simpler:
"asd/asdff/aksdmmf/uuu".split('/')["asd/asdff/aksdmmf/uuu".split('/').length -1]
it will provide you "uuu". Thanks