使用模式从javascript中的字符串获取子字符串
如何从javascript中匹配模式的字符串中获取子字符串? 我有这根绳子使用模式从javascript中的字符串获取子字符串,javascript,Javascript,如何从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-t
<!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>
所以输出必须是,一个值为的数组
["current_date","application_position","applicant_name","staff_name"]
这些值是动态的。这取决于模板有多少特殊代码
此外,这些值必须是唯一的
我尝试了过滤功能,但不起作用。我怎么做?有没有用javascript实现的示例 假设字符串中包含名为
html\u str的变量
var results=html\u str.match(/[^{}]+(?=\})/g)代码>
结果将是一个数组。因此,对于您发布的html,结果将是[“当前日期”、“申请职位”、“申请人姓名”、“员工姓名”]
,这很快。但我找到了解决问题的办法。我将替换子字符串,而不是在html中获取所有特殊代码。所以我不需要得到匹配值。但多亏了答案。
["current_date","application_position","applicant_name","staff_name"]