Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get value after last slash (/) [duplicate]

I can get background image by doing

$('#item').css('background-image').

but the result is 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.

like image 882
aviate wong Avatar asked Apr 13 '15 05:04

aviate wong


People also ask

How do you get the string after the last slash?

Use the . substring() method to get the access the string after last slash.

How do you find the last two values of a string?

To get the last two characters of a string in JavaScript, call the slice() method on the string, passing -2 as an argument. For example, str. slice(-2) returns a new string containing the last two characters of str .

How do I get the last character of a URL?

You can just use substr() for this.


1 Answers

You can use lastIndexOf and substring:

var uri= $('#item').css('background-image');
var lastslashindex = uri.lastIndexOf('/');
var result= uri.substring(lastslashindex  + 1).replace(".png","");

or using .split() and .pop()

var uri= $('#item').css('background-image');
var result=uri.split("/").pop().replace(".png","");
like image 113
Milind Anantwar Avatar answered Oct 11 '22 15:10

Milind Anantwar