Javascript 从最后一个斜杠拆分变量 - jquery

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5555518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:47:02  来源:igfitidea点击:

Split variable from last slash - jquery

javascript

提问by blasteralfred Ψ

I have a variable var1/var2/var3. I want to store var3the part after last slash in a variable and the part before that (var1/var2/) in another variable. How can I do this?

我有一个变量var1/var2/var3。我想将var3最后一个斜杠之后的部分存储在一个变量中,并将 ( var1/var2/)之前的部分存储在另一个变量中。我怎样才能做到这一点?

回答by Mark Coleman

You can use lastIndexOfto get the last variable and that to get the rest.

您可以使用lastIndexOf获取最后一个变量,然后使用它来获取其余变量。

var rest = str.substring(0, str.lastIndexOf("/") + 1);
var last = str.substring(str.lastIndexOf("/") + 1, str.length);

Example on jsfiddle.

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

回答by Chandu

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

回答by Bheru Lal Lohar

var last  = url.split("/").pop();
console.log(last);

You can get last piece of url with array pop method.

您可以使用数组 pop 方法获取最后一段 url。

回答by AlexC

var txt = "var1/var2/var3";
txt = txt.split('/')

var Var1 = txt.pop();
var Var2 = txt[0]+'/'+txt[1];

alert(Var1);
alert(Var2);

回答by Rion Williams

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:

演示:

Demo

演示

回答by Shailesh Sonare

string.substring(start,end)

string.substring(开始,结束)

where

在哪里

start = Required. The position where to start the extraction. First character is at index 0

开始 = 必需。开始提取的位置。第一个字符位于索引 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

回答by RedClover

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
  • )
  • \/– 匹配斜线
  • (?=– 如果后面跟着
    • [^\/]+– 除了斜线之外的任何东西。
    • $– 以及字符串的结尾
  • )

回答by lukastymo

The simplest solution is to use javascript:

最简单的解决方案是使用 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

编辑:但最短的解决方案是:substring()使用