Skip to main content
hank-builds.com

Markdown Link regex

Matches Markdown-style links [text](url).

/\[([^\]]+)\]\(([^)]+)\)/g

What it matches

The inline link form: square brackets around the text, immediately followed by parentheses around the destination. Two capture groups come out of it, the label and the target, which is what makes it useful for rewriting links rather than only finding them.

Railroad diagram

Read left to right. Every path through the diagram is a string the pattern accepts.

\[ Group [^\]] \] \( Group [^)] \)

Drag to pan. Ctrl+scroll to zoom.

Breakdown

gFind all matches instead of stopping after the first
\[Literal "["
[^\]]+Match not one of: Literal "]", one or more
\]Literal "]"
\(Literal "("
[^)]+Match not one of: Literal ")", one or more
\)Literal ")"

Test cases

Matches

  • [Google](https://google.com)
  • [a](b)
  • [text with spaces](/relative/path)

Does not match

  • [missing paren](oops
  • no link here
  • [](empty-label)

Caveats

Neither group allows its own closing delimiter, so a label containing a bracket or a URL containing a parenthesis, common in Wikipedia links, truncates the match. It does not handle the optional title after the URL, reference-style links, or images, which differ only by a leading exclamation mark.

More web patterns

Test this against your own input in the regex tester, browse the full pattern library, or check the syntax cheat sheet.