-4

这是我的脚本:

function createSummaryAndThumb(pID) {
var div = document.getElementById(pID);
var imgtag = "";
var img = div.getElementsByTagName("img");
var summ = summary_noimg;
if(img.length >= 1) {
    imgtag = '<span class="thumbnail pull-left"><img src="' + img[0].srcreplace('/s640/', '/s' + image_size + '/') + '" width="' + img_thumb_width + 'px" height="' + img_thumb_height + 'px"/></span>';
    summ = summary_img;
}

var summary = imgtag + '<p>' + removeHtmlTag(div.innerHTML, summ) + '</p>';
div.innerHTML = summary;
}

上面的脚本将 /s640 替换为 /image_size。但是我能做些什么来改变'640'部分的任何值。我试过这个,但它不会奏效-

function createSummaryAndThumb(pID) {
var div = document.getElementById(pID);
var imgtag = "";
var img = div.getElementsByTagName("img");
var summ = summary_noimg;
if(img.length >= 1) {
    if (uh>img_thumb_width) imgtag = '<span class="thumbnail pull-left"><img src="' + img[0].src.replace('/s' + uh + '/', '/s' + img_thumb_width + '/') + '" width="' + img_thumb_width + 'px" height="' + img_thumb_height + 'px"/></span>';
    summ = summary_img;
}

var summary = imgtag + '<p>' + removeHtmlTag(div.innerHTML, summ) + '</p>';
div.innerHTML = summary;
}
4

2 回答 2

2

您需要将内部的数字与以下内容匹配:

replace(/\/s[0-9]+\//, '/s' + image_size + '/')

这是一个例子:http: //jsfiddle.net/nfDea/

另一种选择是仅捕获开头和结尾,以便连接:

replace(/(\/s)[0-9]+(\/)/, "$1" + image_size + "$2");

http://jsfiddle.net/nfDea/1/

如果您实际上指的是一个名为 的变量uh,那么您需要使用以下内容:

var re = new RegExp("/s(" + uh + ")/");
a = a.replace(re, "/s" + image_size + "/");

http://jsfiddle.net/nfDea/4/

或者

var re = new RegExp("(/s)" + uh + "(/)");
a = a.replace(re, "$1" + image_size + "$2");

http://jsfiddle.net/nfDea/3/

于 2013-01-25T06:45:25.797 回答
-1
var reg = new RegExp('/s' + uh);
...
img[0].src.replace(reg, '/s' + img_thumb_width + '/') 
于 2013-01-25T06:49:23.300 回答