Skip to content

Instantly share code, notes, and snippets.

@elfefe
Created April 14, 2023 12:20
Show Gist options
  • Save elfefe/ef08e583e276e7617cd316ba2382fc40 to your computer and use it in GitHub Desktop.
Save elfefe/ef08e583e276e7617cd316ba2382fc40 to your computer and use it in GitHub Desktop.
Markdown regex

A regex by itself won't be sufficient to fully parse text into Markdown, as Markdown has a variety of syntax rules that can't be captured by a single regular expression. However, I can provide you with a set of regex patterns to help identify some common Markdown elements. You can use these patterns to build a parser or converter:

  1. Headers: ^(#{1,6})\s+(.+)$

  2. Bold text: \*\*(.+?)\*\*|__(.+?)__

  3. Italic text: \*(.+?)\*|_(.+?)_

  4. Strikethrough: ~~(.+?)~~

  5. Inline code: `(.+?)`

  6. Unordered list: ^\s*[-+*]\s+(.+)$

  7. Ordered list: ^\s*\d+\.\s+(.+)$

  8. Blockquote: ^>\s*(.+)$

  9. Code block: ^```(?:\s*(\w+))?([\s\S]*?)^```$

  10. Links: \[(.*?)\]\((.*?)\s?(?:"(.*?)")?\)

  11. Images: !\[(.*?)\]\((.*?)\s?(?:"(.*?)")?\)

Note that these regex patterns do not cover all possible Markdown syntax, and they might not account for some edge cases. To fully parse text into Markdown, you would ideally use a dedicated Markdown parser library for your programming language of choice, such as markdown-it for JavaScript or Python-Markdown for Python.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment