解决图片全部替换问题。在线等
$(function() {
var dataObj = eval(data); blur //转换为json对象
var content = $("#content").text();
for (var d in dataObj) {
content = content.replace(dataObj[d].tag, "<img src=" + dataObj[d].src + " alt='shu'/>");
}
$("#content").html(content);
});
//json
var data=[
{"tag":"[001]","src":"http://style.jctrans.com/tcp/ubb/1.gif"},
{"tag":"[002]","src":"http://style.jctrans.com/tcp/ubb/2.gif"},
{"tag":"[003]","src":"http://style.jctrans.com/tcp/ubb/3.gif"},
{"tag":"[004]","src":"http://style.jctrans.com/tcp/ubb/4.gif"},
{"tag":"[005]","src":"http://style.jctrans.com/tcp/ubb/5.gif"}
]
只能替换一次,希望用正则替换所有
json
function
正则
------解决方案--------------------规则的话直接下面的就行了
content = content.replace(/\[(\d+)\]/g, function ($0, $1) { return "<img src='http://style.jctrans.com/tcp/ubb/1" + parseInt($1) + ".gif' alt='shu'/>" });