-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
  • 319,460
  • 37
  • 290
  • 318
adam78
  • 8,930
  • 20
  • 79
  • 187

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
  • 110,383
  • 23
  • 155
  • 178
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,567
  • 8
  • 18