-2

I have the following image src:

<img src="http://xyz.example.com/abc/def/ghi/someimagename.jpg">

How can I use jquery to extract the image name without the extension e.g. in the above case this would be someimagename?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
adam78
  • 9,668
  • 24
  • 96
  • 207

2 Answers2

1

You can use regex /\/([^\/]+?)\.jpg">/ and get the capturing group value

var str = '<img src="http://xyz.example.com/abc/def/ghi/someimagename.jpg">';
var res = str.match(/\/([^\/]+)\.jpg">/)[1];
document.write(res);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
0

you can do this too.

var string = '<img src="http://xyz.example.com/abc/def/ghi/someimagename.jpg">';
string = string.split("ghi/")[1].split(".")[0];

EDIT:

or you can do the following, which will work pretty good for any kind of url

var string = '<img src="http://xyz.example.com/abc/def/ghi/someimagename.jpg">';
function reverse(s){
    return s.split("").reverse().join("");
}

string = reverse(string).split(".")[1].split("/")[0];
string = reverse(string);
Arif
  • 1,617
  • 9
  • 18