var b = 0; var arr = []; str = str.replace(/J/g,function(){ var args = arguments; b++; arr.push(args[1]); return '#'; }); console.log(str+":共替换" + b + "个,替换位置:" + arr.join()); //#ack Zhao #ust Me:共替换2个,替换位置:0,10