How i can get all links from my html response?

I have body html response. I need to get array list of links from this html

And my code looks like this:

const $ = cheerio.load(pm.response.text());
var script = $("script").text();

// but the following only gives me a list of "https", but I need full link
let authCode = pm.response.text().match(/https:/g); 

console.log("here " + authCode);

help, pls

@orbital-module-cand1 Welcome to the community! :wave:

Instead of regular expressions (if you can) I would recommend using the cheerio library.

For example, if you are looking to extract links from <a> HTML tags href attribute, you can use the following code:

let links = []

$('a').each(function () {
    let href = $(this).attr('href'); 
    if(href && href.startsWith("http")) { // make sure links start with http, to filter out for full URLs
        links.push(href);
    }
});

console.log(links);

regular expressions return exact matches only, and your regular expression only matches the 'https:', to get the full URI you will need to expand the regex to match the entire URL. I encourage you to check out some learning resources online on regular expressions to get a better understanding of how regular expressions work. (If you are in a hurry, a quick search online for regex to match web-address would help you).

2 Likes

Thank you, it works perfect :star_struck:

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.