2

我在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+']');
}                                });

我该怎么做?非常感谢您的帮助

4

1 回答 1

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]);
}
于 2013-04-06T19:41:27.220 回答