<textarea id="e_textarea">
http://forum.csdn.net[/img]
http://forum.csdn.net[/img]
http://forum.csdn.net[/img]
http://forum.csdn.net[/img]
</textarea><input type="button" value="change" onclick="rep2()"><script type="text/javascript">
function rep2(){
var t = document.getElementById("e_textarea").value;
var s = t.replace(/\[\/\img]/g, '');
document.getElementById("e_textarea").value = s;
}
</script>==============================以上代码是把textarea所有[/img]去掉现在我想达到以下效果:
把textarea所有[/img]替換成 &p 加 順序數字 加 = ,而且第一个是2,第二个是3,第三个是4...如此类推...原来:http://forum.csdn.net[/img]
http://forum.csdn.net[/img]
http://forum.csdn.net[/img]
http://forum.csdn.net[/img]想达到的效果以下:
http://forum.csdn.net&p2=
http://forum.csdn.net&p3=
http://forum.csdn.net&p4=
http://forum.csdn.net&p5=

解决方案 »

  1.   

    哥哥第二次回帖,给你写个:
             //源字符串
       var src = ""+"http://forum.csdn.net[/img]"
       +"http://forum.csdn.net[/img]"
       +"http://forum.csdn.net[/img]"
       +"http://forum.csdn.net[/img]";
            //将源字符串分解为数组
       var p = src.split("[/img]");
            //目标字符串
       var dest = "";
       for(var i =1;i<=p.length;i++){
       dest+=p[i]+"&p"+i;
       }
    alert(dest);//
      

  2.   

    function rep2(){
    var t = document.getElementById("e_textarea").value;
    var _ii=2,s = t.replace(/\[\/\img]/g, function(){
        return "&p"+(_ii++);
    });
    document.getElementById("e_textarea").value = s;
    }