巧妙的javascript 正则表达式替换字符串
2012-06-13 10:26 chris-shao 阅读(378) 评论(0) 编辑 收藏 举报<script>
var obj={};
obj["123"]='123的替换';
obj["456"]='456的替换';
var s="abc@123@def@456@";
var Marker = '\@';
s = s.replace(new RegExp(Marker + '([^@]*?)' + Marker, 'g'), function (m, $1) {
alert(m);
return obj[$1];
})
alert(s);
</script>
var obj={};
obj["123"]='123的替换';
obj["456"]='456的替换';
var s="abc@123@def@456@";
var Marker = '\@';
s = s.replace(new RegExp(Marker + '([^@]*?)' + Marker, 'g'), function (m, $1) {
alert(m);
return obj[$1];
})
alert(s);
</script>