venus
venus3y ago

✅ – venus – 08-46 May 24

Hey, I am trying to catch all mdx headings (## First Heading, ### Third Heading, etc.) but only the phrase that comes after ## . Could I get some help with regex that can handle it? Thanks. I've found this one, but sadly doesn't work for my case
^(### |## )(.*)\n
^(### |## )(.*)\n
String: \r\n## First Heading\r\n\r\nLorem Ipsum.\r\n\r\n### Second Heading\r\n\r\nLorem Ipsum Lorem Ipsum.\r\n\r\n### Third Heading\r\n\r\nLorem Ipsum Lorem Ipsum Lorem Ipsum.\r\n
5 Replies
ScriptyChris
ScriptyChris3y ago
you want to ignore new line characters (\r\n)?
venus
venus3y ago
Basically I just want to get all headings like: First Heading Second Heading Third Heading
ScriptyChris
ScriptyChris3y ago
'\r\n## First Heading\r\n\r\nLorem Ipsum.\r\n\r\n### Second Heading\r\n\r\nLorem Ipsum Lorem Ipsum.\r\n\r\n### Third Heading\r\n\r\nLorem Ipsum Lorem Ipsum Lorem Ipsum.\r\n'
.match(/#+ (\w+\s\w+)/g) /* OR: /#+ ([A-Za-z ]+)/g */
.map(fragment => fragment.replace(/[# ]+/, ''));
// ['First Heading', 'Second Heading', 'Third Heading']
'\r\n## First Heading\r\n\r\nLorem Ipsum.\r\n\r\n### Second Heading\r\n\r\nLorem Ipsum Lorem Ipsum.\r\n\r\n### Third Heading\r\n\r\nLorem Ipsum Lorem Ipsum Lorem Ipsum.\r\n'
.match(/#+ (\w+\s\w+)/g) /* OR: /#+ ([A-Za-z ]+)/g */
.map(fragment => fragment.replace(/[# ]+/, ''));
// ['First Heading', 'Second Heading', 'Third Heading']
venus
venus3y ago
Thank you. 😉
reactibot
reactibot3y ago
This question has an answer! Thank you for helping 😄 If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/978579801398997032 This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/978579801398997032