我想把变量str中的<img src="/public/img/face/2.gif" class="faceimg">用正则替换为[em:2],且有几次就替换为几次,数字2有可能是别的数字,求哪位大神给帮忙写一个学习学习?
------解决方案--------------------
var s = [
'<div id="faces">',
'<img src="/public/img/face/1.gif" class="faceimg">',
'<img src="/public/img/face/2.gif" class="faceimg">',
'<img src="/public/img/face/3.gif" class="faceimg">',
'<img src="/public/img/face/4.gif" class="faceimg">',
'<img src="/public/img/face/5.gif" class="faceimg">',
'</div>'
].join(''),
reg = /<img src=.+?(\d+)\.gif[^>]+?>/g;
alert(s);
alert(s.replace(reg,'[em:$1]'));
温馨提示:请注意结贴,不然没人帮你想。
------解决方案--------------------
var str="<img src=\"/public/img/face/2.gif\" class=\"faceimg\">sdfsdf<img src=\"/public/img/face/12.gif\" class=\"faceimg\">sdfwesdsdf<img src=\"/public/img/face/5.gif\" class=\"faceimg\">";
var reg=/<img src="\/public\/img\/face\/(\d+)\.gif" class="faceimg">/ig;
str=str.replace(reg,"[em:$1]");
alert(str);
------解决方案--------------------
var str = '<img src="/public/img/face/2.gif" class="faceimg"><img src="/public/img/face/3.gif" class="faceimg">';
str = str.replace(/<img src="\/public\/img\/face\/(.*?).gif" class="faceimg">/g,'[em:$1]');
console.log(str);