Regex to parse image link in Markdown

Viewed 5289

I'm trying to create regex to parse markdown links.

regex:

!\[[^\]]*\]\((.*)\s"(.*[^"])"?\s*\)

Test (link to live demo):

foo

![](image 2.png "hello world")

bar

Group 1 will be image 2.png, and group 2 will be hello world.

The problem appears when I try to parse a link without title:

foo

![](image 2.png)

bar

How I should modify regex to make it work in both cases?

3 Answers

Here's a complete regexp to match both the Alt text and the image url in a markdown file with a named capture group:

(?<alt>!\[[^\]]*\])\((?<filename>.*?)(?=\"|\))\)

The previously accepted answer only accounts for standard images, it's possible however that images could be used as links for hyperlinks, resulting in a nested image reference, such as:

![alt-text](http://example.com/image.png "image title")](http://example.com/some?target)

A more complete regex pattern would like like this:

\[?(!)(?'alt'\[[^\]\[]*\[?[^\]\[]*\]?[^\]\[]*)\]\((?'url'[^\s]+?)(?:\s+(["'])(?'title'.*?)\4)?\)

This pattern also provides named groups for all the potential other info you might want about the image, such as "alt text" or "title".

Related