日期:2014-05-16 浏览次数:20483 次
<script>
var s = '(12P33)P(P23&P)';
var r1 = /\(.*?\)/g, r2 = /#\$(\d+)\$#/g, i = 0, a = [];
s = s.replace(r1, function($){
a.push($);
return '#$'+ (i++) +'$#';
})
s = s.replace(/p/ig, '你要替换的');
s = s.replace(r2, function($, $1){
return a[$1];
});
alert( s )
</script>
var s = '(12P33)#$0$#P(P23&P)';
var r1 = /\(.*?\)/g, r2 = /\((\d+)\)/g, i = 0, a = [];
s = s.replace(r1, function($){
a.push($);
return '('+ (i++) +')';
});
s = s.replace(/p/ig, '你要替换的');
s = s.replace(r2, function($, $1){
return a[$1];
});
alert( s )