0

How to get string that is present in regular expression.

Input: For example HtmlContent contains string

"<% CustomerSignUpURL ||Text:Hello Abhishek %>"

Output: after replacing string should be anchor tag like below:

Hello Abhishek //  where Hello Abhishek is anchor tag

Please help me with the code what should be there in place of question mark given below:

campaignInfo.HtmlContent = campaignInfo.HtmlContent.replace(
    /<% CustomerSignUpURL ||Text:([a-zA-Z0-9_\-\.]+) %>/g,
    '<a href="<% CustomerSignUpURL %>">{????}</a>'
);
1
  • 1
    I think you're just looking for $1 ? Commented Aug 3, 2021 at 17:08

1 Answer 1

1

First, get the content of the tag by:

const tagContent = campaignInfo
    .HtmlContent
    .match(/CustomerSignUpURL \|\|(.*):(.*)%>/)[2];

// then replace it with:
campaignInfo.HtmlContent = campaignInfo.HtmlContent.replace(
    /<% CustomerSignUpURL \|\|Text:.+?(?=>)./g,
    `<a href="<% CustomerSignUpURL %>">${tagContent}</a>`
);
Sign up to request clarification or add additional context in comments.

1 Comment

what will be the name of anchor tag? I want it to be dynamic text Example: Hello Abhishek

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.