Pattern library / Extract & Replace
Regex to Match Markdown Links
Inline Markdown links ([text](url)) live in every README, docs export and CMS dump, so pulling them out with regex — regex match markdown link, extract url from markdown regex, markdown link regex — is a recurring question. The published pattern is \[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\): group 1 captures the link text, group 2 captures the destination, and the optional tail skips a quoted title so the URL group stays clean. Everything is literals, character classes and groups — no lookahead, lookbehind or backreference — so the same pattern runs unchanged in JavaScript, Python, GA4 and BigQuery.
How the pattern works
- Three moving parts:
\[matches the literal opening bracket,([^\]]+)takes the link text up to the first], then\]\(bridges to the destination where([^)\s]+)takes the URL — no spaces, no parentheses — and the optional(?:\s+"[^"]*")?skips a quoted title without capturing it. Group 1 is the link text, group 2 is the URL. - Find mode returns every link in the document:
[...md.matchAll(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g)].map(m => [m[1], m[2]])gives[['home','https://example.com'],['Docs','../guide/']]for the sample string in “Where to use it” below. Drop thegflag or use.exec()when you only want the first link. - Images are links too:
matches because the match starts at the[, not the!. To skip them RE2-safe, prefix(!?)and filter on group 1 in code — group 1 is!for images and empty for links, leaving text in group 2 and URL in group 3. The one-pattern alternative(?<!!)works in JavaScript and Python but is lookbehind, which RE2 rejects outright. - Titles in single quotes are not covered by the default tail: swap
"for a quote class —(?:\s+["'][^"']*["'])?— and[a](b 'Chapter')then matches with group 2 =b. The published pattern is RE2-safe: literals, classes, groups and quantifiers only, so it runs unchanged in JavaScript, Python, GA4 and BigQuery — see RE2 vs PCRE.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
[home](https://example.com) | Match | a plain inline link: group 1 = home, group 2 = https://example.com | Try → |
[Docs](../guide/) | Match | relative URLs are just as valid as absolute ones | Try → |
See [pricing](https://x.dev/pricing) today | Match | text around the link is ignored — the match starts at the [ | Try → |
[image](pic.png "title") | Match | the optional title is skipped, so group 2 stays pic.png without quotes | Try → |
[wiki](https://en.wikipedia.org/wiki/Foo_(bar)) | Match | matches, but the URL group stops at the first ) — you get .../Foo_(bar, not the full link | Try → |
[empty]() | No match | the URL group needs at least one character — an empty destination does not match | Try → |
[broken](https://x.dev | No match | missing the closing ) — nothing to match | Try → |
no links in this text | No match | no [text](url) structure at all | Try → |
[a [b] c](url) | No match | the text group stops at the first ], so ( never follows a ] — nested brackets break it | Try → |
Where to use it
JavaScript (extract every link as text + href)
const md = '[home](https://example.com) and [Docs](../guide/)';
[...md.matchAll(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g)]
.map(m => ({ text: m[1], href: m[2] }));
// [{ text: 'home', href: 'https://example.com' },
// { text: 'Docs', href: '../guide/' }]
Python (findall → text/URL tuples)
import re
md = '[home](https://example.com) and [Docs](../guide/)'
re.findall(r'\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)', md)
# [('home', 'https://example.com'), ('Docs', '../guide/')]
BigQuery SQL (RE2 — pass the group number explicitly)
SELECT
REGEXP_EXTRACT('[home](https://example.com)',
r'\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)', 1) AS link_text,
REGEXP_EXTRACT('[home](https://example.com)',
r'\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)', 2) AS url;
-- link_text: home | url: https://example.com
Common mistakes
- Link text cannot contain
].[a [b] c](url)does not match: the text group stops at the first], then the pattern wants(but finds a space. Nested brackets are rare in prose but common in code samples — no character-class pattern will help; walk bracket depth in code instead. - URLs containing parentheses are cut at the first
).[wiki](https://en.wikipedia.org/wiki/Foo_(bar))matches, but group 2 stops at.../Foo_(bar— regex cannot count brackets. One link per line? The greedy form\[([^\]]+)\]\((.+)\)runs to the last)and captures the full URL; several links on one line and it over-captures (u1) [b](u2), so for Markdown with balanced parentheses use a parser, not a pattern. - Only inline links match. Reference-style links —
[docs][ref], collapsed[docs][]and bare[docs]— use different syntax and never match this pattern. If you need both forms you need a second pattern or a Markdown parser; do not bolt alternation onto this one blind.
FAQ
How do I extract link text and URL in JavaScript?
Run the pattern in find mode with the g flag and read groups 1 and 2: [...md.matchAll(/\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)/g)].map(m => [m[1], m[2]]) returns every [text, url] pair — [['home','https://example.com'],['Docs','../guide/']] for the sample above. Use m[1] for the anchor text and m[2] for the href.
How do I match or skip images ()?
An image is a link with a ! in front, and the match starts at the [, so  matches too. To filter images RE2-safe, prefix (!?) and drop matches whose group 1 is ! in your code — text and URL then move to groups 2 and 3. The lookbehind (?<!!) does it in one pattern in JavaScript and Python, but RE2 (GA4, BigQuery, Go) rejects look-around — see RE2 vs PCRE.
Does this pattern work in GA4, BigQuery and Go (RE2)?
Yes — it uses only literals, character classes, groups and quantifiers: no lookahead, no lookbehind, no backreference, so there is nothing RE2 refuses. In BigQuery pass the group number explicitly: REGEXP_EXTRACT(md, r'\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)', 2) returns the URL while group 1 is the text. The tester runs the browser engine, so results match Node exactly — check RE2 vs PCRE before shipping any pattern to a Google tool.
Related patterns
URL Regex — Match http/https Links (Practical) · Quoted String Regex — Match "..." and '...' · HTML Tag Regex - Find & Validate Tags
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.