Javascript 获取最后一个斜杠 (/) 后的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29598540/
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 after last slash (/)
提问by aviate wong
I can get background image by doing
我可以通过做得到背景图像
$('#item').css('background-image').
but the result is url('www.example.com/something/123.png')
但结果是 url('www.example.com/something/123.png')
How can get get only the 123 value? I can't use string position because the value of the img name doesn't' necessary is 3 characters.
怎么能只得到 123 的值?我不能使用字符串位置,因为 img 名称的值不需要是 3 个字符。
回答by Milind Anantwar
You can use lastIndexOfand substring:
您可以使用lastIndexOf和substring:
var uri= $('#item').css('background-image');
var lastslashindex = uri.lastIndexOf('/');
var result= uri.substring(lastslashindex + 1).replace(".png","");
or using .split()and .pop()
或使用.split()和.pop()
var uri= $('#item').css('background-image');
var result=uri.split("/").pop().replace(".png","");

