Javascript 如何将字符串解析为angular中的特定模式?

Javascript 如何将字符串解析为angular中的特定模式?,javascript,angular,typescript,Javascript,Angular,Typescript,我有一个angular组件,其中有4个字段customerName,startDate,startTime。现在,我有了第4个字段,即textarea,在该字段中,我向用户显示将通过消息电子邮件或sms发送什么消息。我在组件中定义了一个字符串,如下所示 emailConent = 'Hi [customer_name],↵↵Your appointment has been scheduled for [start_date] at [start_time]' Hi John Doe, Yo

我有一个
angular
组件,其中有4个字段
customerName
startDate
startTime
。现在,我有了第4个字段,即
textarea
,在该字段中,我向用户显示将通过消息电子邮件或sms发送什么消息。我在组件中定义了一个字符串,如下所示

emailConent = 'Hi [customer_name],↵↵Your appointment has been scheduled for [start_date] at [start_time]'
Hi John Doe,

Your appointment has been scheduled for 30-Mar-20 at 03:00 pm.  
Please confirm your appointment.
'Hi [customer_name],↵↵Your appointment has been scheduled for [start_date] at [start_time]. Please be on time there'
因此,在组件加载时,我通过将
[]
中的所有文本替换为输入字段的相应值来生成一条消息。比如我用
customerName
的值替换
[customer\u name]
,比如我用
startDate
替换
[start\u date]
,以此类推。所以我的模板变成这样

emailConent = 'Hi [customer_name],↵↵Your appointment has been scheduled for [start_date] at [start_time]'
Hi John Doe,

Your appointment has been scheduled for 30-Mar-20 at 03:00 pm.  
Please confirm your appointment.
'Hi [customer_name],↵↵Your appointment has been scheduled for [start_date] at [start_time]. Please be on time there'
该模板在textarea字段中可见,用户可以看到该模板。用户也可以更改此文本,就像用户可以在此文本区域中添加附加文本一样

所以我想要的是,如果用户像这样在文本区域中添加更多的数据

Hi John Doe,

Your appointment has been scheduled for 30-Mar-20 at 03:00 pm.  
Please confirm your appointment. Please be on time there
所以你们可以看到,
请准时到那里,
是用户在文本区添加的文本。所以我想把它解析回我的标签格式

emailConent = 'Hi [customer_name],↵↵Your appointment has been scheduled for [start_date] at [start_time]'
Hi John Doe,

Your appointment has been scheduled for 30-Mar-20 at 03:00 pm.  
Please confirm your appointment.
'Hi [customer_name],↵↵Your appointment has been scheduled for [start_date] at [start_time]. Please be on time there'
我只想获取用户在
textarea
中添加的附加文本


如何在
angular
中实现这一点?

您应该使用typescript中支持的模板文本字符串。 你应该使用“``”,而不是使用
。 如果要打印变量,可以通过以下方式非常简单:

`Hi ${customer_name},

 Your appointment has been scheduled for ${start_date} at ${start_time}. Please be on time there`

这是我能想到的最基本的差异查找器。您可以修改差异计算器

函数getEmail(客户名称、开始日期、结束日期){
const emailTemplate=`Hi${customer_name},
您的约会已安排在${start_date}下午${end_date}开始。
请确认您的预约;
返回电子邮件模板;
}
const myString=getEmail(“John Doe”,“30-Mar-20”,“03:00”);
//修改后
const userString=`Hi John Doe,
您的约会安排在20年3月30日下午3:00。
请确认您的预约。请准时到那里;
让diff=“”;
const myStringArray=myString.split(/\n/);
userString.split(/\n/).some((行,索引)=>{
常量originalLine=myStringArray[index].trim();
const modifiedLine=line.trim();
if(修改线!==原始线){
差异=
原始线.长度>修改线
?原基线。替换(修改后的基线,“”)
:modifiedLine.替换(原始行“”);
返回false;
}
});

控制台日志(diff)