
Key takeaways
- The pattern with two capture groups returns each link URL and its text, but only on small, predictable HTML you have tested it against first.
- Add the s flag when link text can wrap, because in JavaScript the dot does not match a line break by default.
- Regex cannot tell a commented-out link from a real one, and it struggles with unquoted attributes and nested tags inside the link.
- For HTML you do not control, DOMParser or BeautifulSoup reads links reliably in a couple of lines and handles every case the regex misses.
A regular expression can pull links out of a small, predictable piece of HTML, and the pattern below finds the URL and the link text of each anchor tag with two capture groups. It breaks on real-world pages, though: link text that spans lines, links inside comments, unquoted attributes and nested tags all trip it up. For a quick one-off extraction, test the pattern on your own sample first. For anything that runs on unknown HTML, use an HTML parser instead.
This guide gives the pattern, runs it through a live regex tester on a sample with the usual traps in it, and shows the parser-based alternative.
The pattern
<a\s[^>]*?href=["']([^"']+)["'][^>]*>(.*?)</a>
Use it with the flags gi (all matches, ignore case). Read left to right, it means:
<a\san opening anchor tag followed by whitespace, so it does not match<abbr>or<article>.[^>]*?any other attributes before href, as few characters as possible, without leaving the tag.href=["']the href attribute with either a double or a single quote.([^"']+)capture group 1: the URL, everything up to the next quote.["'][^>]*>the closing quote, any attributes after href, and the end of the tag.(.*?)</a>capture group 2: the link text, lazily, up to the first closing</a>.
Testing it on a sample with traps in it
Open the free regex tester, type the pattern without the surrounding slashes, set the flags to gi and paste this test string:
<p>Read the <a href="https://example.com/docs">docs</a> or <a class="btn" href='/pricing'>see pricing</a>.</p>
<a href="/blog">Latest
posts</a>
<!-- <a href="/old-page">Old page</a> -->
The tester reports 3 matches found, and the Match Details list shows:
Match 1: "<a href="https://example.com/docs">docs</a>" at index 12 (Groups: https://example.com/docs, docs)
Match 2: "<a class="btn" href='/pricing'>see pricing</a>" at index 59 (Groups: /pricing, see pricing)
Match 3: "<a href="/old-page">Old page</a>" at index 149 (Groups: /old-page, Old page)
Two things went wrong. The /blog link is missing, and a commented-out link that no visitor can see was counted.

Why the /blog link was missed
In JavaScript, the dot does not match a line break unless you add the s flag (dotAll). The link text "Latest" and "posts" sits on two lines, so (.*?) stops at the line break and the match fails. Change the flags to gis and the tester shows a fourth match: the /blog anchor at index 111, with the groups /blog and the two-line text "Latest posts".
Notice that the line break inside the tag's attributes was never a problem: [^>] already matches newlines, because a negated character class matches any character that is not listed, line breaks included.
Cases the pattern still gets wrong
| Input | What happens | Fix inside regex? |
|---|---|---|
| Link text on two lines | Missed without the s flag | Yes, add s |
| Link inside an HTML comment | Matched, though it is not a real link | Only by stripping comments first |
| Unquoted href, such as href=/about | Missed | Possible, but the pattern gets much longer |
A > inside an attribute value |
The tag is cut short | No, not reliably |
Text with nested tags, such as <a href="/x"><b>Bold</b></a> |
Group 2 includes the <b> tags |
Strip tags from the group afterwards |
| Links added by JavaScript | Not in the HTML source at all | No, read the rendered page instead |
HTML is not a regular language: elements nest, attributes can appear in any order, and quotes, comments and scripts all change what a < means. A pattern can cover the cases you have seen, but there is always another valid page it gets wrong.
The dependable way: let a parser read the HTML
In a browser or any JavaScript environment with a DOM, two lines do the job and handle every case in the table except script-added links on a page you fetched as text:
const doc = new DOMParser().parseFromString(html, 'text/html');
const links = [...doc.querySelectorAll('a[href]')].map(a => [a.getAttribute('href'), a.textContent.trim()]);
Comments are ignored, quotes do not matter, nested tags are flattened by textContent, and multi-line text is just text. In Python, BeautifulSoup does the same with soup.find_all('a', href=True). If you are on the command line, most languages have an HTML parser in the standard library or a well-known package.
When the regex is still the right tool
Regex is fine when the HTML is yours and predictable: a generated sitemap page, an export from a tool you control, or a quick search through a saved file in your editor. In those cases:
- Paste a real sample into the regex tester and check the match count against what you expect.
- Add s if any link text may wrap, and i if tags may be uppercase.
- Read the Groups column for each match, not just the highlight, to confirm group 1 is the URL.
- Click Copy all matches to copy every matched tag one per line, or tick Test replacement and enter
$1to see the text with each anchor replaced by its URL.
If you run both the regex and a parser on the same page, paste the two link lists into the text compare tool to see exactly which links the regex missed or added; our guide on comparing two texts to find differences walks through it. Links with spaces or non-ASCII characters can be cleaned up with the URL encoder.
Sources
Frequently asked questions
A workable pattern is <a\s[^>]*?href=["']([^"']+)["'][^>]*>(.*?)</a> with the flags gi. Group 1 is the URL and group 2 is the link text. Add the s flag if link text can wrap onto a second line.
The most common reason is link text that spans two lines, because the dot does not match a line break unless the s flag is on. Unquoted href values and a > character inside an attribute also make the pattern fail.
Use a parser such as DOMParser in JavaScript or BeautifulSoup in Python for any HTML you do not control. Regex is fine for small, predictable HTML you generated yourself, after testing it on a real sample.
A regex only sees characters, so it cannot tell that a tag sits between comment markers. Strip comments first, or use a parser, which ignores commented-out tags automatically.
