Hi i have a markdow text like the one below and i want to slice it in H2 tilte, H2 content
## **Intro**
* bla bla
* bla bla bla
## Tortilla
* chico
* chica
### 1. sub-section
* and another bla.
Regex result should be :
title 1:
Intro
content 1:
* bla bla
* bla bla bla
title 2:
Tortilla
content 2:
* chico
* chica
### 1. sub-section
* and another bla.
I tried with this regex
/^## (?<title>.*)(?<content>.*(?:n(?!##).*)*)/gm
But doesn’t catch the sub-section content.
Can someone help please?
2
Answers
The regex below produces the output desired from the input you have given.
Explanation of most pieces:
^##s
(?<title>.*)n
(?:(?!##s).*n?)+
The primary issue with your attempt is that you are trying to account for the new line after
title
by including it incontent
when it just needs to be discarded.The secondary issue is that you’re not providing a differentiator for lines that start with
##
that are not H2 (i.e. there must be whitespace after the##
)Note: the optional new line (
n?
) at the end ofcontent
is required when the input does not end with a new lineI would choose an approach based on the combination of a simple regex like …
/^##s(.*)/gm
… which will be utilized forsplit
ting the markdown string. The relevant array data then getsreduce
d into the final result, an array which features all theH2
related items, each item consisting of a sanitizedtitle
value and a likewise sanitizedcontent
value …