Skip to content
Advertisement

Javascript – Extract a link from inside an html code

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"
*/

Advertisement

Answer

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);
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement