<p>Some people using the Cross Validated Stack Exchange website for the first time have difficulty formatting their sections headers.</p>
<blockquote>
<h3>This is an Example of a Well-Formatted Section Header</h3>
</blockquote>
<p>In particular, there are people who have trouble with the idea that one single tiny space character <code>⎵</code> before for after a <code>#</code> could make the section header not render correctly.</p>
<pre><code>#Approach Using a $χ^{2}$ Distribution# 
# Approach Using a $χ^{2}$ Distribution # 
</code></pre>
<blockquote>
<p>###Approach Using a <span class="math-container">$χ^{2}$</span> Distribution###</p>
<h3>Approach Using a <span class="math-container">$χ^{2}$</span> Distribution</h3>
</blockquote>
<p>The feature request is that headers render correctly regardless of the number of white-spaces characters to the left or right of the pound signs.</p>
<p>The following is a somewhat reasonable regular expression for section headers.</p>
<p>Feel free to amend my suggestion if you think that you can improve upon it.</p>
<blockquote>
<h1>Level One Header <code>(?:^\s*)(#[^\n\r]+#?)(?:$)</code></h1>
<h1>Level Two Header <code>(?:^\s*)(##[^\n\r]+#?)(?:$)</code></h1>
<h1>Level Three Header <code>(?:^\s*)(###[^\n\r]+#?)(?:$)</code></h1>
</blockquote>
<hr />
<blockquote>
<ul>
<li><p><code>(?:^\s*)</code></p>
<ul>
<li><code>(</code> ....... begin group</li>
<li><code>?:</code> ....... annotate group as a non-capturing group</li>
<li><code>^</code> ....... start of string</li>
<li><code>\s*</code> ....... zero or more white space carriage return, tab, new line, and more
<ul>
<li><code>\s</code> ....... any one white space character such as carriage return, tab, new line, and more</li>
<li><code>*</code> ....... zero or more</li>
</ul>
</li>
</ul>
</li>
<li><p><code>(#[^\n\r]+#?)</code></p>
<ul>
<li><p><code>(</code> begin group</p>
</li>
<li><p><code>#</code> this is a number sign or pound signs or hashtags</p>
</li>
<li><p><code>[^\n\r]+</code>  one or more of anything other than newline and carriage return</p>
<ul>
<li><code>[</code> begin character class</li>
<li><code>^</code> negate character class (invert the class)</li>
<li><code>\n\r</code> ...... newline and carriage return</li>
<li><code>]</code> .... end character class</li>
<li><code>+</code>  one or more</li>
</ul>
</li>
<li><p><code>#?</code> zero or one number sign or pound signs or hashtags</p>
<ul>
<li><code>#</code> number sign or pound signs or hashtags</li>
<li><code>?</code> zero or one</li>
</ul>
</li>
<li><p><code>)</code> .... end group</p>
</li>
</ul>
</li>
<li><p><code>(?:$)</code> non-capturing group for end of string</p>
<ul>
<li><code>?:</code> non-capturing</li>
<li><code>$</code> end of string</li>
</ul>
</li>
</ul>
</blockquote>
