Regular Expressions: Markdown to HTML anchors

Matt Kenefick
1 min readJul 30, 2021

Our traditional Markdown link has a syntax that looks like this:

[I'm an inline-style link](https://www.google.com)

But HTML anchors have a syntax that looks like this:

<a href="https://www.google.com">I'm an inline-style link</a>

How can we easily convert it from one to another? Regular expressions ❤

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

Breakdown

Here’s what all those arrows and brackets means for this expression:

Example in JavaScript

The JavaScript example is pretty concise because of the replace prototype offered by the language. Be careful when you use a lot of escape sequences combined with forward slash delimiters.

Example in PHP

The PHP is nearly as concise as the JavaScript example and utilizes the preg_replace function which you can read about here.

Example in C#

The C# example is a little more verbose than the previous two due to the way we’re handling string formatting in this example. There’s nothing wrong with being verbose.

--

--

Matt Kenefick

Owner at @PolymerMallard. Previously at @Vimeo and @BigSpaceship.