php - How to match Markdiwn's H1~H6 using regular expressions?
某草草
某草草 2017-06-14 10:49:55
0
2
720

Simply use /(#{1,6}) *(\S ) *(?:\r|\n)*/ can indeed match the H1~H6 titles in Markdown. But it also matches the reference with the # (such as a comment) in the code block, such as:

A paragraph of text

Comments using >

or

# 使用四个空格缩进的注释

# Comments using `

// code... # 代码注释
# 使用"的注释

How to exclude these situations that do not require matching?

某草草
某草草

reply all(2)
滿天的星座

As far as your question is concerned. /^#/ seems to be able to meet the needs, but it cannot support languages ​​​​annotated with # such as php and bash.

So if you want to be accurate enough, you need to parse md into chunks.

过去多啦不再A梦

I think your question is that the rules for commented code blocks and titles are the same, how to distinguish them.

Two ways to distinguish

  • Title changing rules

  • Code blocks are placed in fixed tags

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template