如何在打字稿中编写字符串正则表达式取 'href' 值?

How to write a string regix in typescript take 'href' value?

我需要从以下模式 html 文本中获取 'href'(标记 link 位置值)值。需要专家帮助才能使用 typescript

字符串文本一

"<html><body><a href="C:\Users\K\Documents\docker_command.txt">docker_command.txt </a></body></html>"

字符串文本二

 "<html><body><a href="https://www.facebook.com/">https://www.facebook.com/ </a></body></html>"

是这样的吗?

const a = '"<html><body><a href="C:\Users\K\Documents\docker_command.txt">docker_command.txt </a></body></html>"';
const b = '"<html><body><a href="https://www.facebook.com/">https://www.facebook.com/ </a></body></html>"';

function getHref(html: string): string|null {
  if (!html) {
    return null;
  }

  return html.match(/ href=("|')([^'"]*?)('|")/i)[2];
}

console.log(getHref(a));
console.log(getHref(b));