如何使用 cheerio 替换所有 "a" 标签的域名?

How to replace domain name for all "a" tag using cheerio?

我正在尝试弄清楚如何使用 cheerio 替换所有“a”标签的域名,但是 $(this).attr("href") returns 未定义

function changeDomain(html) {
  const $ = cheerio.load(html);
  $("a").each((_, node) => {
    console.log($(this).attr("href")); // undefined
  });
  return $.html();
}

我要改变

由此

 <a href="HTTP://google.com/home">Home</a>

至此

 <a href="HTTP://example.com/home">Home</a>

因为你用的是arrow function,他们没有得到this

改用$(node)

for(let a of $('a').get()){
  $(a).attr('href', $(a).attr('href').replace(/old/, 'new'))
}