使用基于json数组的正则表达式替换字符串中的JavaScript

使用基于json数组的正则表达式替换字符串中的JavaScript,javascript,regex,json,Javascript,Regex,Json,我需要用in{}替换所有,用使用正则表达式的JSON数组中的数据替换{}本身,并且需要区分大小写 {}s的数量可能会因页面而异 s ="<address><div>{Name}</div><div>{Address1}</div><div>{Address2}</div><div>{ZipCode} {City}</div><div>{State}</div><

我需要用in
{}
替换所有,用使用正则表达式的JSON数组中的数据替换
{}
本身,并且需要区分大小写

{}
s的数量可能会因页面而异

s ="<address><div>{Name}</div><div>{Address1}</div><div>{Address2}</div><div>{ZipCode} {City}</div><div>{State}</div><div>{Country}</div><div>{ContactName}</div></address>"

"DeliveryAddress":
        {
            "Id":5637169131,
            "Name":"some name",
            "Name2":null,
            "Address":"Somewhere street 12",
            "Address2":null,
            "City":"Heven",
            "ZipCode":"1111",
            "State":"FL",
                        "Country":"US",
            "VatNo":null,
            "ContactRecId":"Receiving Department",
            "ContactName":null,
            "ContactPhone":null,
            "ContactEmail":null,
            "DefaultShopDeliveryAddress":false,
            "DefaultServiceDeliveryAddress":false,
            "IsOneTime":false,
            "CaptureId":"00000000-0000-0000-0000-000000000000"
        },
s=“{Name}{Address1}{Address2}{ZipCode}{City}{State}{Country}{ContactName}”
“交货地址”:
{
“Id”:5637169131,
“名字”:“某个名字”,
“名称2”:空,
“地址”:“某处街12号”,
“地址2”:空,
“城市”:“海文”,
“ZipCode”:“1111”,
“州”:“FL”,
“国家”:“美国”,
“VatNo”:空,
“ContactRecId”:“接收部门”,
“ContactName”:空,
“联系人电话”:空,
“ContactEmail”:空,
“DefaultShopDeliveryAddress”:false,
“DefaultServiceDeliveryAddress”:false,
“IsOneTime”:假,
“CaptureId”:“00000000-0000-0000-0000-00000000”
},
完成后,我需要让json数组中的匹配元素替换匹配的{tag}

范例

<address><div>some name</div><div>Somewhere street 12</div><div></div><div>1111 Heaven</div><div>FL</div><div>US</div><div></div></address>
一些名称omewhere street 121111 HeavenFLUS
您可以使用以下代码:

s ="<address><div>{Name}</div><div>{Address1}</div><div>{Address2}</div><div>{ZipCode} {City}</div><div>{State}</div><div>{Country}</div><div>{ContactName}</div></address>"
DeliveryAddress = {
            "Id":5637169131,
            "Name":"some name",
            "Name2":null,
            "Address1":"Somewhere street 12",
            "Address2":null,
            "City":"Heven",
            "ZipCode":"1111",
            "State":"FL",
            "Country":"US",
            "VatNo":null,
            "ContactRecId":"Receiving Department",
            "ContactName":null,
            "ContactPhone":null,
            "ContactEmail":null,
            "DefaultShopDeliveryAddress":false,
            "DefaultServiceDeliveryAddress":false,
            "IsOneTime":false,
            "CaptureId":"00000000-0000-0000-0000-000000000000"
        };

for (var key in DeliveryAddress) {
  var r = new RegExp('{' + key + '}', "i");
  s = s.replace(r, DeliveryAddress[key]);
}
console.log(s);
s=“{Name}{Address1}{Address2}{ZipCode}{City}{State}{Country}{ContactName}”
送货地址={
“Id”:5637169131,
“名字”:“某个名字”,
“名称2”:空,
“地址1”:“某处街12号”,
“地址2”:空,
“城市”:“海文”,
“ZipCode”:“1111”,
“州”:“FL”,
“国家”:“美国”,
“VatNo”:空,
“ContactRecId”:“接收部门”,
“ContactName”:空,
“联系人电话”:空,
“ContactEmail”:空,
“DefaultShopDeliveryAddress”:false,
“DefaultServiceDeliveryAddress”:false,
“IsOneTime”:假,
“CaptureId”:“00000000-0000-0000-0000-00000000”
};
for(交付地址中的var键){
var r=newregexp('{'+key+'}',i”);
s=s.replace(r,交货地址[键]);
}
控制台日志;
输出:

<address><div>some name</div><div>Somewhere street 12</div><div></div><div>1111 Heaven</div><div>FL</div><div>US</div><div></div></address>
一些名称omewhere street 121111 HeavenFLUS

使用字符串。替换为设置的全局和多行标志:

function s_fill(s, dict) {
    for (var n in dict) {
        var re = new RegExp('{' + n + '}', 'gm');
        s = s.replace(re, dict[n]);
    }
    return s;
}