Skip to main content
AllDevToolsHub
Back to all patterns

Markdown Link Extraction

Extraction

Extracts Markdown inline links, capturing both the link text and the URL.

/\[([^\[\]]+)\]\((https?:\/\/[^\s)]+)\)/g

How it works

This pattern matches Markdown inline link syntax [text](url). The first capture group captures the link text (no nested brackets), and the second capture group captures the URL (must start with http or https). Use with the global flag to find all links in a document.

Test Cases

Should Match

  • [Google](https://google.com)
  • [Click here](http://example.com/path?q=1)

Should NOT Match

  • [no url here]
  • (https://no-text.com)
  • [text](ftp://not-http.com)

Quick Summary

Extracts Markdown inline links [text](url) with two capture groups: link text and URL. Use the global flag to find all links in a document. Only matches http/https URLs.

Key Takeaways

Key Takeaways

  • Capture group 1: link text (no nested brackets allowed)
  • Capture group 2: the URL (http/https only)
  • Does not match reference-style links [text][ref] or image links ![alt](url)
  • Use the global flag (g) to extract all links from a document
Use Cases

When to use it

  • Extracting all external links from Markdown documentation
  • Building a link checker for a static site generator
  • Converting Markdown links to HTML anchor tags
Watch out

Common Mistakes

  • Forgetting the global flag when processing a full document
  • Expecting this to match image links, add ! before \[ to match ![alt](url)
  • Not handling reference-style links [text][ref], those need a separate pattern
FAQ

Markdown Link Extraction, Frequently Asked

How do I also match image links?

Prepend an optional ! to the pattern: !?\[([^\[\]]+)\]\((https?:\/\/[^\s)]+)\)

How do I replace all Markdown links with HTML?

Use String.replace() with the regex and a replacement function: str.replace(pattern, (_, text, url) => `<a href='${url}'>${text}</a>`)