Regex patterns/Text & extraction

Markdown link

Two captures — the label and the target — with negated classes instead of lazy dots.

/\[([^\]]+)\]\(([^)]+)\)/g
Open it in Rex

The problem

Pull the label and the URL out of markdown links so you can rewrite or list them.

How it reads

\[1[^\]]repeat\]\(2[^)]repeat\)

Follow the line from left to right — every path you can trace is a string this pattern matches.

  1. \[([^\]]+)\]\(([^)]+)…In order:
  2. \[The character "["
  3. ([^\]]+)Capture group 1:
  4. [^\]]+Any character except "]", one or more times, as many as possible
  5. \]The character "]"
  6. \(The character "("
  7. ([^)]+)Capture group 2:
  8. [^)]+Any character except ")", one or more times, as many as possible
  9. \)The character ")"

Matches

  • [the docs](https://example.com/docs)
  • [home](/)

Does not match

  • plain text
  • [unclosed(https://x)
  • not a [link] here

Where it bites

  • A negated class ([^\]]+) says "anything up to the closing bracket" directly. A lazy .+? gets the same answer by backtracking, which is slower and harder to reason about.
  • Nested brackets in the label, and parentheses inside the URL, both defeat it — markdown allows them and this pattern cannot.
  • Image syntax (![alt](src)) matches too, since the leading ! is not excluded.

More text & extraction patterns