0

I am implementing a Markdown to HTML converter and I found myself in a situation where I don't know if one regex will suffice.

It is implied in the Markdown guide that you can escape backticks in a code block by surrounding the block with two backticks.

For instance, the following Markdown text:

``Use `code` in your Markdown file.``

should be converted to the following HTML:

<code>Use `code` in your Markdown file.</code>

My first guess for implementing this is to

  1. Find all of the code blocks with double backticks
  2. Replace the double backticks with the <code> tag
  3. Do the same with single backticks, except the ones already inside a <code> tag

Is there any way to create a pattern that matches text between two backticks but not inside <code> tags ? Or am I overthinking it and there is a simpler solution ?

Thomas Ferro
  • 2,362
  • 19
  • 32
  • Have you considered using [commonmark](https://commonmark.org/) instead? It has [libraries](https://github.com/commonmark/commonmark-spec/wiki/list-of-commonmark-implementations#go) and comes with a spec. – zerkms Nov 27 '19 at 09:34
  • I'm making my own implementation for a training purpose, but thanks for the ressources ! – Thomas Ferro Nov 27 '19 at 09:43
  • 2
    Regular expressions are rarely the answer to lexing/parsing because most languages are not regular languages. Have you seen ["Lexical Scanning in Go" by Rob Pike](https://talks.golang.org/2011/lex.slide#1) yet? – Peter Nov 27 '19 at 09:51
  • Not yet but I sure will ! – Thomas Ferro Nov 27 '19 at 09:54
  • Looks like you are looking to create a regex, but do not know where to get started. Please check [Reference - What does this regex mean](https://stackoverflow.com/questions/22937618) resource, it has plenty of hints. Also, refer to [Learning Regular Expressions](https://stackoverflow.com/a/2759417/3832970) post for some basic regex info. Once you get some expression ready and still have issues with the solution, please edit the question with the latest details and we'll be glad to help you fix the problem. – Wiktor Stribiżew Nov 27 '19 at 10:39
  • the dup is wrong imho, OP is asking to parse a markdown syntax, as mentioned, regexp are rarely a solution to this problem. however i did not find a suitable stackoverflow topic that introduces this subject. –  Nov 27 '19 at 12:04
  • Yes, I was probably wrong to induced my first solution with regexp as the question. – Thomas Ferro Nov 27 '19 at 12:18

0 Answers0