Advertisement

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.

The pattern
\[([^\]]+)\]\(([^)\s]+)(?:\s+"[^"]*")?\)
Load this pattern into the tester →

How the pattern works

Test it: match & no-match examples

Every row is loaded straight into the tester — click Try to see the match live:

InputResultWhy
[home](https://example.com)Matcha plain inline link: group 1 = home, group 2 = https://example.comTry →
[Docs](../guide/)Matchrelative URLs are just as valid as absolute onesTry →
See [pricing](https://x.dev/pricing) todayMatchtext around the link is ignored — the match starts at the [Try →
[image](pic.png "title")Matchthe optional title is skipped, so group 2 stays pic.png without quotesTry →
[wiki](https://en.wikipedia.org/wiki/Foo_(bar))Matchmatches, but the URL group stops at the first ) — you get .../Foo_(bar, not the full linkTry →
[empty]()No matchthe URL group needs at least one character — an empty destination does not matchTry →
[broken](https://x.devNo matchmissing the closing ) — nothing to matchTry →
no links in this textNo matchno [text](url) structure at allTry →
[a [b] c](url)No matchthe text group stops at the first ], so ( never follows a ] — nested brackets break itTry →

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

Test this pattern on your own data
Paste it into the free tester with your real strings — nothing leaves your browser.
Open the regex tester →

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 (![alt](src))?

An image is a link with a ! in front, and the match starts at the [, so ![logo](x.png) 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.

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.