我建议在正则表达式上使用HTML解析器,但是这里仍然是一个正则表达式,它将
href根据每个链接的属性值创建捕获组。它将匹配使用双引号还是单引号。
<as+(?:[^>]*?s+)?href=(["'])(.*?)1
您可以在此处查看此正则表达式的完整说明。
摘要游乐场:
const linkRx = /<as+(?:[^>]*?s+)?href=(["'])(.*?)1/;const textToMatchInput = document.querySelector('[name=textToMatch]');document.querySelector('button').addEventListener('click', () => { console.log(textToMatchInput.value.match(linkRx));});<label> Text to match: <input type="text" name="textToMatch" value='<a href="google.com"'> <button>Match</button> </label>


