0

I'm trying to extract the link inside href and store it in a variable. Please see my code snippet below. Note that the token id is expected to be different with every single run. Thank you for your help.

const body = <html><head></head><body><p>This is SignUp Email with confirmation link</p><p><a href = "http://www.company.com/ls/click?upn=tokenid-11111-22222-333333-444444-555555-xxxxxx"></a></p></body></html>
const activation_link = ???
console.log(activation_link)
/*
The expected result to be printed on Console:
"http://www.company.com/ls/click?upn=tokenid-11111-22222-333333-444444-555555-xxxxxx"
*/
4
  • 1
    What have you tried? Where are you getting stuck? Commented Dec 3, 2021 at 9:53
  • Is it how you define body or it's just a "representation" of what you have in the document? Asking because it's not proper way of doing it. Commented Dec 3, 2021 at 9:54
  • Following on from the comment above, if this is just an example of your document: Will there ever be multiple links on the page? and if so, how do you specify which URL you want? Commented Dec 3, 2021 at 9:56
  • @TheTanadu this is actually just a code snippet. My main concern is/was to find how to easily get a link from inside html body. Commented Dec 3, 2021 at 10:06

1 Answer 1

1

const body = `<html><head></head><body><p>This is SignUp Email with confirmation link</p><p><a href = "http://www.company.com/ls/click?upn=tokenid-11111-22222-333333-444444-555555-xxxxxx"></a></p></body></html>`
const matched_links = body.match(/(?<=")http.+(?=")/);
console.log(matched_links);

Sign up to request clarification or add additional context in comments.

Comments

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.