如何从javascript中的字符串中获取与模式匹配的子字符串? 我有这个字符串,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<body bgcolor="#efefef"
style="background: #efefef; margin: 0; padding: 0; width: 100%; -webkit-font-smoothing: antialiased; font-family: 'Roboto', sans-serif; font-weight: 400;">
<table width="100%" cellspacing="0" cellpadding="0" border="0" style="width: 600px; margin: 0 auto; padding: 0;">
<tbody>
<tr>
<td width="100%">
<table width="100%" cellspacing="0" cellpadding="0" border="0"
style="width: 600px; margin: 0 auto; padding: 20px 0 12px 0;">
<tbody>
<tr>
<td width="50%">Webmail App
</td>
<td width="50%">
<div style="color: #5d5d5d; font-size: 14px; font-weight: bold; letter-spacing: 1px; font-family: 'Helvetica Neue',Arial,Helvetica,sans-serif; text-align: right;">
{{current_date}}
</div>
</td>
</tr>
</tbody>
</table>
<div style="width: 600px; margin: 0 auto; border: 1px solid #CCCCCC; background-color: #FFFFFF;">
<div style="background-color: #0065b2; color: #fbfbfb; font-size: 18px; font-weight: bold; padding: 16px 12px 16px 12px; font-family: 'Helvetica Neue',Arial,Helvetica,sans-serif;">
Application for {{application_position}}
</div>
<div style="background-color: #4676fd; color: #efefef; font-size: 16px; font-weight: bold; letter-spacing: 1px; padding: 16px 12px; border-bottom: 1px solid #CCCCCC; font-family: 'Helvetica Neue',Arial,Helvetica,sans-serif;">
{{applicant_name}}
</div>
<div style="background-color: #fffff; color: #0E2334; font-size: 18px; font-weight: bold; padding: 24px 12px 8px 12px; font-family: 'Helvetica Neue',Arial,Helvetica,sans-serif;">
this is a sample message.
</div>
<div style="background-color: #fffff; color: #0E2334; text-align: left;">
</div>
<div style="background-color: #fffff; color: #0E2334; text-align: left; padding: 24px 12px 8px 12px; font-size: 13px; font-family: 'Helvetica Neue',Arial,Helvetica,sans-serif; letter-spacing: 0.5px;">
Thanks,<br/>{{staff_name}}</div>
</div>
<div style="width: 600px; margin: 0 auto; padding: 0 0 20px 0;"><p
style="color: #666; font-size: 11px; letter-spacing: 0.5px; font-weight: 500; text-decoration: none; font-family: 'Helvetica Neue',Arial,Helvetica,sans-serif;">
Thank you for using Webmail<br/> </div>
</td>
</tr>
</tbody>
</table>
</body>
</html>
这是一个字符串。我在电子邮件模板中使用它。如何获得与之匹配的所有子字符串
{{something_code}}
因此输出必须是一个值为
的数组["current_date","application_position","applicant_name","staff_name"]
这些值是动态的。它取决于模板有多少特殊代码。
值也必须是唯一的。
我尝试过滤功能但不起作用。我该怎么做?任何样本在javascript中做到这一点?
答案 0 :(得分:0)
假设您的字符串包含名为html_str
var results = html_str.match(/[^{{}}]+(?=\})/g);
results
将是一个数组。因此,对于results
发布的html,["current_date","application_position","applicant_name","staff_name"]