13

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.

aviate wong
  • 745
  • 2
  • 6
  • 10

1 Answers1

38

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","");
Milind Anantwar
  • 81,290
  • 25
  • 94
  • 125