1

I am trying to remove part of the name of the image to create thumbnails.

The image name is created dynamically ie HG57JWY_0.jpg, HG57JWY_1.jpg, HG57JWY_2.jpg. I managed to remove the .jpg using:

var imageName = $('#slider1 li img').prop('src').match(/(\w*)\.\w{3,4}$/)[1];

My problem is that i cant figure how to remove the "_0"

return '<img src="sample-img/'+ imageName +'_' + [i -+ 1] + '.jpg">';

this generates HG57JWY_0_0.jpg

Any ideas how i do that?

Edit: Forgot to mention that big image and the thumb are in different folder. So i display the big image in one place holder with the src = http://mydomain.com/sample-img/imagename.jpg

and i place the thumbs in a list with the src = http://mydomain.com/sample-img-thumb/imagename.jpg

Edit 2:

Thanks to Alexander, i used my original code

 var imageName = $('#slider1 li img').prop('src').match(/(\w*)\.\w{3,4}$/)[1];

and added his .split("_")[0]

var imageName = $('#slider1 li img').prop('src').match(/(\w*)\.\w{3,4}$/)[1].split("_")[0];

This takes the whole image src ie http://mydomain/imageFolder/image_1.jpg, and returns just the "image".

Then i was able to use what ever path i want with

return '<img src="sample-img/'+ imageName +'_' + [i -+ 1] + '.jpg">';

and just changed the sample-img/ with what i needed.

4

4 回答 4

4

Use split.

var imageName = $('#slider1 li img').prop('src').split("_")[0];

UPDATE

You could use split in your match:

var imageName = $('#slider1 li img').prop('src')
                                    .match(/(\w*)\.\w{3,4}$/)[1]
                                    .split("_")[0];

Or, modifying your regular expression (as shown also in Dave Rager's answer) to exclude the matching of the pattern _number:

var imageName = $('#slider1 li img').prop('src').match(/(\w+)_\d+\.\w{3,4}$/)[1]
于 2012-09-19T19:42:26.653 回答
1

If the image name guaranteed to contain "_x" then try:

var imageName = $('#slider1 li img').prop('src').match(/(\w*)_\d+\.\w{3,4}$/)[1];
于 2012-09-19T19:42:51.163 回答
1
$('#slider1 li img').prop('src', function(i, value) {
    return value.substr(0, value.indexOf('_'));
});
于 2012-09-19T19:42:58.373 回答
0
var imageName = $('#slider1 li img').prop('src').match(/(\w*)(_[0-9]+)\.\w{3,4}$/)[1];
于 2012-09-19T19:44:01.160 回答