更改图像的URL字符串的一部分

时间:2014-11-20 18:24:01

标签: javascript jquery html image replace

我想知道是否有办法使用jQuery更改URL字符串中的最后一部分(图像名称)。

这就是我所拥有的:

http://domain.com/m/m62RLwnqkpPuKl13jSxURBg/80.jpg

在这里我们/我需要它:

http://domain.com/m/m62RLwnqkpPuKl13jSxURBg/140.jpg

问题更新:

我需要定位和替换图像的名称,在这种情况下为“80”而不使用其余的URL,因为每个图像的URL路径都不同。

<div class="image">
<img alt="" src="http://thumbs3.ebaystatic.com/m/m62RLwnqkpPuKl13jSxURBg/80.jpg" itemprop="image">
</div>

2 个答案:

答案 0 :(得分:3)

replace

var s='http://domain.com/m/m62RLwnqkpPuKl13jSxURBg/80.jpg';

s=s.replace(/(.*)\/.*(\.jpg$)/i, '$1/40$2')

alert(s);

FIDDLE

答案 1 :(得分:0)

好的,我找到了解决方案。如果寻找相同的东西,这里是:

$('.image').children().each(function () {
    $(this).html(function (i, html) {
        return $(this).html().replace(/80.jpg/g, '140.jpg');
    });
});

归功于@Eliseu Monar

谢谢!

相关问题