我已经使用这个作为测试目的:
var temp = '
sdfdf | sds |
var occurence_count = temp.match(/td class/g).length;
for(i = 1; i<=occurence_count; i++){
temp = temp.replace(new RegExp('td class', ''), 'td id="my_cusom_id-' + i+'" class');
}
console.log(temp);
而且因为它应该是显示结果。 在我的情况下,我得到了occurence_count = 2
或者你可以用它张贴作为一个答案在修改的方式解决
$.get("untouchable-url-on-my-host",function(data,status){
var $data = $(data);
$data.find('td').each(function(){
$(this).attr('id', 'my_custom_id_'+ $(this).index());
$("#content").append($data);
});
}
你可以试试这个解决方案
$.get("untouchable-url-on-my-host",function(data,status){
var temp = data;
var occurence_count = temp.match(/td class/g).length;
for(i = 1; i<=occurence_count; i++){
temp = temp.replace(new RegExp('td class', ''), 'td id="my_custom_id-' + i+'" class');
}
$("#content").html(temp);
}); });