The Pattern
``regex
\[([^\]]+)\]\(([^)\s]+)(?:\s+"([^"]*)")?\)
`
Flags: g
What It Accepts and Rejects
| Input | Result |
|---|---|
[Utiliki](https://utiliki.com) | ā matches |
[titled](https://example.com "Title") | ā matches |
 | ā matches |
[broken link(no-closing-bracket.com) | ā rejected |
Three Capture Groups
Group 1 is the link text, group 2 the URL, group 3 the optional title. The text class
[^\]]+ matches anything that is not a closing bracket, and the URL class [^)\s]+
stops at a space or the closing parenthesis.
Note that the image syntax  also matches ā the leading ! is simply not
part of the pattern. Distinguishing links from images needs (? before the bracket,
where lookbehind is supported.
The Limits of This Pattern
Markdown is not a regular language, and links are where that bites hardest. A URL containing
a parenthesis ā very common in Wikipedia links ā terminates the match early. Nested brackets
in link text break group 1. Reference-style links ([text][ref]) use a different syntax
entirely and never match.
For extracting links from a handful of files this is fine. For anything that must be
correct, parse with a CommonMark implementation and walk the AST.
Testing Before Shipping
A regex that has only been tried against inputs you expect to match is untested. Every
pattern needs three kinds of case:
1. Valid inputs that should match, including the awkward-but-legal ones.
2. Invalid inputs that should not, especially near-misses that differ by one character.
3. Adversarial inputs ā very long strings, unusual Unicode, and nesting that could
trigger catastrophic backtracking.
Paste your own examples into the tester above and watch which lines highlight. A pattern
that matches everything you throw at it is usually too permissive rather than correct.
Catastrophic Backtracking
Nested quantifiers over overlapping character classes ā (a+)+, (\w+\s?)* ā can take
exponential time on a non-matching input. On a server that is a denial-of-service bug, not a
performance issue. If a pattern is applied to user input, bound the input length first and
prefer explicit alternation over nested repetition.
Anchors, Greediness and Backtracking
Three behaviours account for most regex surprises, and this pattern shows all three.
Anchors. ^ and $ pin the match to the start and end of the input. Without them,
\d{3} matches the 123 inside abc123def. With m in the flags ā as here ā they
pin to each *line* instead, which is what lets one pattern be tested against a list.
Greediness. .* takes as much as it can and gives back only when forced; .*? takes
as little as possible. On , the pattern <.*> matches the whole string and
<.*?> matches just .
Testing It Properly
`javascript
const pattern = /\[([^\]]+)\]\(([^)\s]+)(?:\s+"([^"]*)")?\)/g;