DD
DevDash

Regex for Markdown Heading

Regex Pattern

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

Markdown heading (# to ######)

Quick Answer

The regex pattern for markdown heading is `^#{1,6}\s+.+$`. Markdown heading (# to ######). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
# Title✓ Matches
## Subtitle✓ Matches
###### Deep heading✓ Matches
### My Section✓ Matches
#NoSpace✗ No match
Not a heading✗ No match
####### Too deep✗ No match

Code Examples

javascript

const regex = /^#{1,6}\s+.+$/;
const isValid = regex.test(value);

python

import re
pattern = r'^#{1,6}\s+.+$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^#{1,6}\s+.+$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^#{1,6}\s+.+$/', $value)) {
    echo "valid";
}

java

String pattern = "^#{1,6}\\s+.+$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.