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);
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).