Escaping Characters in Markdown

Markdown logo

Markdown uses various special characters like asterisks, underscores, and brackets for formatting. But often you need to use these characters literally rather than using them to format text. This is where character escaping comes in.

The escape character

To display a character other than for Markdown formatting, add a backslash (\) before it. This is called escaping the character.

Characters that can be escaped

The following characters can be escaped with a backslash:

CharacterDescription
*asterisk
\backslash
`backtick (see escaping backticks in code section_)
_underscore
( )parentheses
{ }curly braces
< >angle brackets
[ ]square brackets
#hash mark
+plus sign
-minus sign (hyphen)
.dot
!exclamation mark
|pipe

Escaping asterisks and underscores

In Markdown, an asterisk or underscore is used for emphasis:

Syntax:

*Italic text*
**Bold text**

Result:

Italic text
Bold text

To display the asterisks and underscores literally we use the escape character \:

Syntax:

\* None Italic text \*
\** None Bold text \**

Result:

* None Italic text *
** None Bold text **

Escaping backticks

Backticks are used to create inline code in Markdown:

Syntax:

`const name = 'Homer';`

Result:

const name = 'Homer';

Add the backslash to display backticks literally,:

Syntax:

\`This is not code\`

Result:

`This is not code`

Escaping hash marks

Hash marks are used to create headings in Markdown:

Syntax:

### This is a level 4 heading

Result:

This is a level 4 heading

To display the hash mark(s) literally:

Syntax:

\#### Escaping a level 4 heading

Result:

#### Escaping a level 4 heading

Square brackets and parentheses are commonly used to create links in Markdown. To include them literally without creating a link, add a backslash:

Syntax:

[This text is a link](https://learn-webdevelopment.com)
\[This text is not a link](https://learn-webdevelopment.com)

Result:

This text is a link
[This text is not a link](https://learn-webdevelopment.com)

Escaping images

To show the image syntax without actually creating an image:

Syntax:

\!\[This is not an image](/markdown_logo.svg)

This example compares the syntax to a regular image:

![This is an image](/markdown_logo.svg)
\!\[This is not an image](/markdown_logo.svg)

Result:

This is an image
![This is not an image](/markdown_logo.svg)

Escaping a backslash

If you need to display a literal backslash, escape it with another backslash:

Syntax:

\\ escaping a backslash

Result:

\ escaping a backslash

Alternative Approaches

In some cases, instead of escaping individual characters, you might prefer to use code:

  • Inline code - For shorter, single-line text with special characters.
  • Code blocks - For multiple lines of text that contain many special characters.

For example:

Syntax:

`# Using code syntax to show a hash mark without escaping`

Result:

# Using code syntax to show a hash mark without escaping