Scan an HTML string for exact anchor tag substrings
A newsletter editor lets writers paste HTML from other tools. Before sending, a link checker needs every link in the draft, exactly as written, so it can show each one in a review panel and point at the original markup. Parsing with the DOM is not an option here: the checker runs in a worker, and browsers rewrite markup when they parse it (quotes, casing, whitespace), so the snippets would no longer match the source.
Implement extractAnchors(html). It scans an HTML string and returns every complete anchor element, from its <a opening tag through its closing tag, as an exact substring of the input, in the order they appear.
extractAnchors(html) // => string[]
<a in any case (<A too), immediately followed by whitespace (space, tab or newline) or >. So <abbr>, <area>, <aside> and <article> are not anchors.> that is not inside a quoted attribute value. Values can use double or single quotes, and can contain >, for example <a title="a > b" href="/x">.</a> in any case, with optional whitespace before the > (</A>, </a >). </abbr> and </aside> are not closing tags for an anchor.<strong>) is part of it.<!-- and the next -->, including anchors inside it. A comment that is never closed runs to the end of the string.[] if there are no complete anchors, and also if html is not a string.extractAnchors('Read <a href="/guide">the guide</a> or <A HREF=\'/faq\'><b>FAQ</b></A >.');
// => ['<a href="/guide">the guide</a>', '<A HREF=\'/faq\'><b>FAQ</b></A >']
extractAnchors('<abbr title="HyperText">HTML</abbr> <a title="x > y" href="#cmp">compare</a>');
// => ['<a title="x > y" href="#cmp">compare</a>']
extractAnchors('<!-- <a href="/old">old</a> --><a href="/new">new</a>');
// => ['<a href="/new">new</a>']
extractAnchors('Draft: <a href="/later">unfinished');
// => []