0
$\begingroup$

Some people using the Cross Validated Stack Exchange website for the first time have difficulty formatting their sections headers.

This is an Example of a Well-Formatted Section Header

In particular, there are people who have trouble with the idea that one single tiny space character before for after a # could make the section header not render correctly.

#Approach Using a $χ^{2}$ Distribution# 
# Approach Using a $χ^{2}$ Distribution # 

###Approach Using a $χ^{2}$ Distribution###

Approach Using a $χ^{2}$ Distribution

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.

The following is a somewhat reasonable regular expression for section headers.

Feel free to amend my suggestion if you think that you can improve upon it.

Level One Header (?:^\s*)(#[^\n\r]+#?)(?:$)

Level Two Header (?:^\s*)(##[^\n\r]+#?)(?:$)

Level Three Header (?:^\s*)(###[^\n\r]+#?)(?:$)


  • (?:^\s*)

    • ( ....... begin group
    • ?: ....... annotate group as a non-capturing group
    • ^ ....... start of string
    • \s* ....... zero or more white space carriage return, tab, new line, and more
      • \s ....... any one white space character such as carriage return, tab, new line, and more
      • * ....... zero or more
  • (#[^\n\r]+#?)

    • ( begin group

    • # this is a number sign or pound signs or hashtags

    • [^\n\r]+ one or more of anything other than newline and carriage return

      • [ begin character class
      • ^ negate character class (invert the class)
      • \n\r ...... newline and carriage return
      • ] .... end character class
      • + one or more
    • #? zero or one number sign or pound signs or hashtags

      • # number sign or pound signs or hashtags
      • ? zero or one
    • ) .... end group

  • (?:$) non-capturing group for end of string

    • ?: non-capturing
    • $ end of string
$\endgroup$
1

0

You must log in to answer this question.

Browse other questions tagged .