我在javascript中有一个对象 - :
var array = {"1":"John","2":"Caprio","3":"David","4":"Edward"}
我想这样做 - :
var message = 'Wats up David#1. Are you with David#5 or Caprio#89';
$.each(array, function(key, value){
var matchMe=value+'#'+anywholenumber;
if(message.match(matcheMe))
{
var ge = new RegExp(matchMe, 'g');
message = message.replace(ge,'['+matchMe+']');
} });
我该怎么做?非常感谢您的帮助
答案 0 :(得分:0)
如果代码正常工作,您所拥有的代码将使用第一个元素(john)替换每个数字。
我想你想要这个:
var arr = array("John","Caprio","David","Edward","Suzy");
var message = 'Wats up David#1. Are you with David#5 or Caprio#2';
for (i=0; i < arr.length; i++) {
var re = new RegExp(i, 'g');
message = message.replace(re, arr[i]);
}