gpt4 book ai didi

javascript - 动态替换字符串中标签的所有实例

转载 作者:行者123 更新时间:2023-11-27 23:42:24 25 4
gpt4 key购买 nike

尝试使用 JavaScript 中的 RegExp 替换电子邮件模板中的每个数据标记。以下是电子邮件示例:

<h3>Appointment Scheduled</h3>
<p>%clientName%,</p>
<p>Your appointment with %userName% for %serviceName% is scheduled on %appointmentStart%. Please visit the following link if you would like to view or modify your appointment:</p>
<p style="margin-left: 25px;">%appointmentLink%<br></p>

我正在使用 Meteor 并使用此函数在服务器上执行此操作,但我不明白为什么它不替换每个标签。

prepareEmail : function(templateType, appointmentId) {

var thisUser = Meteor.user();
var message = Emails.findOne({type : templateType, orgId : thisUser.org._id });
var appointment = Appointments.findOne();
var user = Meteor.users.findOne(appointment.userId);
var service = Services.findOne("nNChpXMtYrWsyrqCr");
var client = "Client Name";

var variables = [
{ name: 'userName', value : '%userName%', collection : 'Meteor.users', field : thisUser.profile.name},
{ name: 'serviceName', value : '%serviceName%', collection : 'Services', field : service.name},
{ name: 'serviceDuration', value : '%serviceDuration%', collection : 'Services', field : service.duration},
{ name: 'servicePrice', value : '%servicePrice%', collection : 'Services', field : service.price},
{ name: 'serviceDescription', value : '%serviceDescription%', collection : 'Services', field : service.description},
{ name: 'appointmentStart', value : '%appointmentStart%', collection : 'Appointments', field : appointment.start},
{ name: 'appointmentEnd', value : '%appointmentEnd%', collection : 'Appointments', field : appointment.end},
{ name: 'appointmentLink', value : '%appointmentLink%', collection : 'Appointments', field : appointment._id},
{ name: 'clientName', value : '%clientName%', collection : 'Clients', field : 'name'},
{ name: 'clientPhone', value : '%clientPhone%', collection : 'Clients', field : 'phone'},
{ name: 'clientEmail', value : '%clientEmail%', collection : 'Clients', field : 'email'},
{ name: 'orgName', value : '%orgName%', collection : 'Orgs', field : 'name'},
{ name: 'orgAddress', value : '%orgAddress%', collection : 'Orgs', field : 'address'},
{ name: 'orgPhone', value : '%orgPhone%', collection : 'Orgs', field : 'phone'},
{ name: 'orgEmail', value : '%orgEmail%', collection : 'Orgs', field : 'email'},
];

var emailContent = message.emailContent;
console.log(typeof emailContent)
variables.forEach(function (variable) {
var match = variable.value;
var replace = variable.field;
console.log(match + " --> " + replace);
replaceAll(emailContent,match,replace);
});

console.log(emailContent);
}

function replaceAll(str, find, replace) {
return str.replace(new RegExp(escapeRegExp(find), 'g'), replace);
}
function escapeRegExp(str) {
return str.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");
}

最佳答案

只需更换您的

replaceAll(emailContent,match,replace);

emailContent = replaceAll(emailContent,match,replace);

您忘记保存修改。

参见the fiddle

关于javascript - 动态替换字符串中标签的所有实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33568097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com