从正则表达式替换函数中获取字符串



如何获取正则表达式中存在的字符串。

输入>:例如HtmlContent包含字符串
"<% CustomerSignUpURL ||Text:Hello Abhishek %>"

:替换字符串后应该是锚标记,如下所示:

Hello Abhishek //  where Hello Abhishek is anchor tag

请帮我写一下代码,下面给出的问号应该是什么:

campaignInfo.HtmlContent = campaignInfo.HtmlContent.replace(
/<% CustomerSignUpURL ||Text:([a-zA-Z0-9_-.]+) %>/g,
'<a href="<% CustomerSignUpURL %>">{????}</a>'
);

首先,通过:

获取标签的内容
const tagContent = campaignInfo
.HtmlContent
.match(/CustomerSignUpURL ||(.*):(.*)%>/)[2];
// then replace it with:
campaignInfo.HtmlContent = campaignInfo.HtmlContent.replace(
/<% CustomerSignUpURL ||Text:.+?(?=>)./g,
`<a href="<% CustomerSignUpURL %>">${tagContent}</a>`
);

最新更新