EDDYMENS

Published 10 months ago

How To Change All Markdown Bold Titles Into Headers In VS Code

replacing bold titles with headers in VS Code [→]

Let's say you have a relatively large number of bold titles i.e.: **Title** you would like to change to header titles i.e.:# Title.

A basic search and replace will not cut it so we will need to search using regular expressions [↗].

In this writeup I assume you already have VS Code [↗] editor installed.

  • Open up the markdown document in VS Code.
  • Open the find & replace panel using Ctrl+F.
  • In the find panel paste in the following regex \*\*(.+?)\*\*
  • Click on the [.*] option to activate regex mode.
  • Click on the > button to the left side of the find panel to open up the replace box.
  • Paste ## $1 into the replacement box.
  • Now hit any of the two buttons next to the replace box to begin replacing. The second button will replace all at once.

Breakdown of the Regex

To match all text that have the format **somethinghere** we used the regex string \*\*(.+?)\*\*.

Let's strip away the backslashes for a minute: **(.+?)** In the cleaned-up version, you will notice we have the asterisks laid out similarly to the structure of our boldened texts. That is because we want an exact match of those asterisks.

(.+?) on the other hand specifies that there can be anything in between those asterisks.

The backslash is used to "escape" the asterisk character since it is a regex symbol. This ensures that the asterisks are treated as part of our search and not a regex symbol.

Now in the replacement box, we used ## $1. The double hash ## is the syntax for the level 2 header in markdown. Which is what we would like to have and $1 represents whatever was found in between **[]**.

Here is another article you might like 😊 How to center an image in markdown