Skip to content

Regex for Markdown Link — Pattern, Examples & Caveats

Pattern flags: g
\[([^\]]+)\]\(([^)\s]+)\)

This pattern matches inline Markdown links of the form [text](url), capturing the link text and the destination URL in separate groups. It is used to extract or convert Markdown links to HTML. Web patterns deal with markup, styling and web identifiers: HTML tags and comments, CSS colors, Markdown links, slugs and source-code comments. They are handy for quick scanning and lightweight transforms, but markup is not a regular language, so anything structural or security-sensitive should use a real parser (DOM, CSS or Markdown) rather than regex alone. It uses the g flag(s). Apply it across text to collect every occurrence. It does not handle reference-style links ([text][ref]) or titles ([text](url "title")) — extend the pattern for those. URLs containing spaces or parentheses break the simple [^)\s]+ class; real Markdown parsers handle escaping. Image syntax ![alt](src) also contains a link-like structure; exclude the leading ! if you want only links. As with any regular expression, confirm its behavior in your target engine, test boundary and adversarial inputs, and watch for catastrophic backtracking before relying on it in production.

Advertisement
Ad slot · ref-top

Token-by-token explanation

  • \[ — literal opening square bracket
  • ([^\]]+) — capture the link text (anything but ])
  • \] — literal closing square bracket
  • \( — literal opening parenthesis
  • ([^)\s]+) — capture the URL (no spaces or closing paren)
  • \) — literal closing parenthesis

What it matches

[Qoder](https://qoder.com)[text](path/page)[a b](http://x.y/z)

What it does not match

[text] (url)[text](url with space)![img](x.png)[text]

When to use it

Reach for a web pattern for quick scanning and lightweight transforms of markup, styling and web identifiers: locating tags, comments, colors, slugs or Markdown links. For anything structural or security-sensitive — parsing a DOM, sanitizing HTML, rewriting CSS — use a real parser or a proven library instead, because markup is not a regular language and regex-based handling is a frequent source of bugs and cross-site scripting bypasses.

Caveats

  • It does not handle reference-style links ([text][ref]) or titles ([text](url "title")) — extend the pattern for those.
  • URLs containing spaces or parentheses break the simple [^)\s]+ class; real Markdown parsers handle escaping.
  • Image syntax ![alt](src) also contains a link-like structure; exclude the leading ! if you want only links.

Code examples

JavaScript
const re = /\[([^\]]+)\]\(([^)\s]+)\)/g;
const text = "see [Qoder](https://qoder.com) and more";
const found = [...text.matchAll(re)].map(m => m[1] ?? m[0]);
// found holds every extracted occurrence
Python
import re

rx = re.compile(r"\[([^\]]+)\]\(([^)\s]+)\)")
text = "see [Qoder](https://qoder.com) and more"
found = rx.findall(text)   # every occurrence (groups if present)
Java
import java.util.regex.*;

Pattern p = Pattern.compile("\\[([^\\]]+)\\]\\(([^)\\s]+)\\)");
Matcher m = p.matcher("[Qoder](https://qoder.com)");
boolean ok = m.find();   // scan for occurrences
PHP
<?php
$subject = "[Qoder](https://qoder.com)";
if (preg_match_all('/\[([^\]]+)\]\(([^)\s]+)\)/', $subject, $m)) {
    // $m[0] is the full match; $m holds every occurrence
}

How to test it

Before trusting any pattern, test it against three kinds of input: valid examples that must match, invalid examples that must be rejected, and edge cases at the boundaries (empty strings, maximum lengths, unusual separators and Unicode). Paste the expression into a live regex tester with your actual sample data and confirm the highlighted matches are exactly what you intend. Watch for catastrophic backtracking by trying long non-matching inputs, and remember that engine differences (JavaScript, Python, Java, PCRE, Go/RE2) mean a pattern that works in one may fail or behave differently in another — especially lookarounds, which Go's RE2 does not support at all.

Frequently asked questions

How do I capture the text and URL separately?

The two capturing groups do exactly that: group 1 is the link text ([^\]]+) and group 2 is the URL ([^)\s]+), accessible in your replacement or match result.

Does it match Markdown images?

Images use ![alt](src); the leading ! means this pattern would match the [alt](src) portion. Add a negative lookbehind (?<!!) to exclude images if needed.

When should I avoid regex for web content?

For parsing HTML, CSS or Markdown structure, use a real parser. Regex cannot handle nesting, quoted delimiters or malformed markup reliably, and regex-based sanitization is a common source of XSS bypasses.

Why is regex HTML sanitization dangerous?

HTML has nested, quoted and malformed constructs that regex mis-parses, so attackers hide scripts in forms the pattern misses. Always sanitize with a proven library (DOMPurify) or a real parser, never ad-hoc regex.

Advertisement
Ad slot · ref-in-article

Try the Regex Tester

Live regex matching with groups, flags and explanations.

Open tool →