Verified 2026-09-18

Markdown Heading

Matches Markdown heading lines from level one through six.

Use this to find ATX-style Markdown headings in editors and content-processing scripts.

regexmarkdowncontent

Find

^#{1,6}\s+.*$

Find pattern (regex)

^#{1,6}\s+.*$

The pattern targets headings beginning with one to six hash characters followed by whitespace. It does not parse setext headings or Markdown syntax inside code fences.

Two subtleties: up to three leading spaces are legal before a heading, and closing hashes (## Heading ##) are allowed by the spec, so ^#{1,6} matches but a ^#{1,6}$ check would not. Inside fenced code blocks, hash lines are content, not headings, so process fences first.

Common mistakes

  • Running it over code blocks without excluding fenced code.
  • Assuming every Markdown parser implements exactly the same heading extensions.
Permalink: https://merginit.com/reference/regex/markdown-heading