我有如下字符串数组:
{客户:2046}购买了产品{产品:6473}
{客户:2046}购买了产品{产品:5243}
{客户:37}向{客户:1501}购买了产品{产品:6333}
如果我只是在循环外初始化正则表达式,则某些数组将被跳过映射,如下所示:
彼得买了一把产品椅子
{客户:2046}购买了产品{产品:5243}
John向Susan购买了一张产品表
我必须在第二个位置添加正则表达式,以便所有数组都将被映射?怎么了
let activities = [];
var tkRegex = /{\s*(?:customer):(\d+)\s*}/g;
var pfRegex = /{\s*(?:product):(\d+)\s*}/g;
activities = dActivities.map((act, key) => {
if(act.hasOwnProperty('task_id')){
let task = dTasks.find(el => el.id === act.task_id);
if(task){
let matches = tkRegex.exec(act.template)
if (matches && matches[1] == act.task_id) {
act.template = act.template.replace(matches[0], task.name);
}
}
}
// tkRegex = /{\s*(?:customer):(\d+)\s*}/g;
// pfRegex = /{\s*(?:product):(\d+)\s*}/g;
// Why do I have to assign the value here again so the regexp works
as I expect?
if(act.hasOwnProperty('profile_ids')){
act.profile_ids.map((pId, key) => {
let pf = dProfiles.find(el => el.id === pId);
if(pf){
let matches = pfRegex.exec(act.template);
if (matches && matches[1] == pId) {
act.template = act.template.replace(matches[0], pf.abbreviated_name);
}
}
});
}
return act.template;
});