正则 带变量的替换

1.
for(var num=1;num<20;num++)
{
var re=new   RegExp("\\\[face"+num+"\\\]","gm");  
aa=aa.replace(re,"http://imgs.soufun.com/pic/UserAlbum/mk"+num+".gif");
}


2. 返回匹配结果

<script>
var aa="[face1][face2][face3][face4][face5][face6][face1][face1]werwerwerewr";

//for(var num=1;num<20;num++)
//{
//var re=new   RegExp("\\\[face.*?\\\]","gm");
var re=/\[face.*?\]/g; 
//aa=aa.replace(re,"http://imgs.soufun.com/pic/UserAlbum/mk"+num+".gif");
var tmp=aa.match(re);
//}
for(var i=0;i<tmp.length;i++)
{
aa=aa.replace(tmp[i],"http://imgs.soufun.com/pic/UserAlbum/mk"+tmp[i].substring(5,tmp[i].length-1)+".gif")

}
alert(aa);
</script>


你可能感兴趣的:(正则)