PatternRegex
Match a Markdown link
Match a Markdown link and split it into its display text and URL, handy for auditing docs or building a link checker.
Last updated
Matches
- Check the [docs](https://example.com) for details
- See [our repo](/path/to/repo) here
Doesn’t match
- Just a [bracketed] phrase without a link
- Visit https://example.com directly
Why it’s written this way
The first capture group ([^\]]+) sits inside escaped brackets \[ \] and grabs the link text, everything up to the next closing bracket. The second, ([^)\s]+), sits inside escaped parentheses and grabs the URL, stopping at whitespace or the closing paren.
Splitting the match into two groups lets calling code pull the display text and the destination separately instead of re-parsing the whole matched string.
Edge cases to know
- →A URL containing a literal space, such as an unencoded file path, breaks the match early because [^)\s]+ stops at the first whitespace it meets.
- →It doesn't distinguish a real link from an image embed, which uses a leading ! before the same bracket-paren shape — both match identically here.
- →Nested brackets in the link text, like [a [b] link](url), aren't handled — the character class stops at the first closing bracket it finds.