在 JavaScript 中获取斜杠后的字符串值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8376525/
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
Get value of a string after a slash in JavaScript
提问by r0skar
I am already trying for over an hour and cant figure out the right way to do it, although it is probably pretty easy:
我已经尝试了一个多小时,但无法找到正确的方法,尽管这可能很容易:
I have something like this : foo/bar/test.html
我有这样的事情: foo/bar/test.html
I would like to use jQuery to extract everything after the last /
. In the example above the output would be test.html
.
我想使用 jQuery 提取最后一个/
. 在上面的示例中,输出将为test.html
.
I guess it can be done using substr
and indexOf()
, but I cant find a working solution.
我想可以使用substr
and来完成indexOf()
,但我找不到可行的解决方案。
回答by T.J. Crowder
At least three ways:
至少三种方式:
A regular expression:
一个正则表达式:
var result = /[^/]*$/.exec("foo/bar/test.html")[0];
...which says "grab the series of characters not containing a slash" ([^/]*
) at the end of the string ($
). Then it grabs the matched characters from the returned match object by indexing into it ([0]
); in a match object, the first entry is the whole matched string. No need for capture groups.
...它表示“[^/]*
在字符串 ( $
)的末尾获取不包含斜杠的一系列字符” ( )。然后它通过索引它从返回的匹配对象中获取匹配的字符 ( [0]
); 在匹配对象中,第一个条目是整个匹配的字符串。不需要捕获组。
Using lastIndexOf
and substring
:
使用lastIndexOf
和substring
:
var str = "foo/bar/test.html";
var n = str.lastIndexOf('/');
var result = str.substring(n + 1);
lastIndexOf
does what it sounds like it does: It finds the index of the lastoccurrence of a character (well, string) in a string, returning -1 if not found. Nine times out of ten you probably want to check that return value (if (n !== -1)
), but in the above since we're adding 1 to it and calling substring, we'd end up doing str.substring(0)
which just returns the string.
lastIndexOf
做它听起来像的事情:它在字符串中找到最后一次出现的字符(好吧,字符串)的索引,如果没有找到则返回 -1。十有八九你可能想检查返回值 ( if (n !== -1)
),但在上面,因为我们给它加 1 并调用子字符串,我们最终会做的str.substring(0)
只是返回字符串。
Using Array#split
使用 Array#split
Sudhir and Tom Walters have this covered hereand here, but just for completeness:
Sudhir 和 Tom Walters 在这里和这里都有介绍,但只是为了完整性:
var parts = "foo/bar/test.html".split("/");
var result = parts[parts.length - 1]; // Or parts.pop();
split
splits up a string using the given delimiter, returning an array.
split
使用给定的分隔符拆分字符串,返回一个数组。
The lastIndexOf
/ substring
solution is probablythe most efficient (although one always has to be careful saying anything about JavaScript and performance, since the engines vary so radically from each other), but unless you're doing this thousands of times in a loop, it doesn't matter and I'd strive for clarity of code.
该lastIndexOf
/substring
解决方案可能是最有效的(尽管人们总是要小心说关于JavaScript和表现什么,因为发动机不同,所以从根本上相互),但除非你是在一个循环中这样做上千次,这不是没关系,我会努力使代码清晰。
回答by Tom Walters
You don't need jQuery, and there are a bunch of ways to do it, for example:
你不需要 jQuery,有很多方法可以做到,例如:
var parts = myString.split('/');
var answer = parts[parts.length - 1];
Where myString contains your string.
其中 myString 包含您的字符串。
回答by kasdega
var str = "foo/bar/test.html";
var lastSlash = str.lastIndexOf("/");
alert(str.substring(lastSlash+1));
回答by Sudhir Bastakoti
Try;
尝试;
var str = "foo/bar/test.html"; var tmp = str.split("/"); alert(tmp.pop());
回答by DaveAlger
When I know the string is going to be reasonably short then I use the following one liner... (remember to escape backslashes)
当我知道字符串会很短时,我会使用以下一个衬里...(记住要转义反斜杠)
// if str is C:\windows\file system\path\picture name.jpg
alert( str.split('\').pop() );
alert pops up with picture name.jpg
警报弹出 picture name.jpg
回答by Jawad Zeb
String path ="AnyDirectory/subFolder/last.htm";
int pos = path.lastIndexOf("/") + 1;
path.substring(pos, path.length()-pos) ;
Now you have the last.htmin the path string.
现在您在路径字符串中有last.htm。
回答by Shailesh Sonare
light weigh
重量轻
string.substring(start,end)
where
在哪里
start = Required
. The position where to start the extraction. First character is at index 0`.
start = Required
. 开始提取的位置。第一个字符位于索引 0`。
end = Optional
. The position (up to, but not including) where to end the extraction. If omitted, it extracts the rest of the string.
end = Optional
. 结束提取的位置(最多但不包括)。如果省略,它将提取字符串的其余部分。
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 S. Domeng
Jquery:
查询:
var afterDot = value.substr(value.lastIndexOf('_') + 1);
Javascript:
Javascript:
var myString = 'asd/f/df/xc/asd/test.jpg'
var parts = myString.split('/');
var answer = parts[parts.length - 1];
console.log(answer);
Replace '_' || '/' to your own need
替换'_' || '/' 根据您自己的需要
回答by Vishal Kumar
As required in Question::
根据问题要求::
var string1= "foo/bar/test.html";
if(string1.contains("/"))
{
var string_parts = string1.split("/");
var result = string_parts[string_parts.length - 1];
console.log(result);
}
and for question asked on url (asked for one occurence of '=' )::
[http://stackoverflow.com/questions/24156535/how-to-split-a-string-after-a-particular-character-in-jquery][1]
以及在 url 上提出的问题(要求出现一次 '=' )::
[ http://stackoverflow.com/questions/24156535/how-to-split-a-string-after-a-particular-character-in -jquery][1]
var string1= "Hello how are =you";
if(string1.contains("="))
{
var string_parts = string1.split("=");
var result = string_parts[string_parts.length - 1];
console.log(result);
}