-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?

4
  • I would recommend that you use this library: link it has what you need. Commented Nov 23, 2015 at 8:45
  • "http://xyz.example.com/abc/def/ghi/someimagename.jpg".split('/').pop() Commented Nov 23, 2015 at 8:49
  • 1
    How did you manage to ignore all suggestions of duplicates? Also string handling is not jQuery, just plain JS. Commented Nov 23, 2015 at 8:50
  • .split('/').pop().split('.').shift(); Commented Nov 23, 2015 at 8:50

2 Answers 2

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);

Sign up to request clarification or add additional context in comments.

2 Comments

Why use lazy quantifier ([^\/]+? in /\/([^\/]+?)\.jpg">/) with a negated character class? The result is the same as with a greedy one.
@stribizhev thanks :)
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);

1 Comment

It does not have jpg extension, recheck please. about URL you might be right

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.