I'm currently trying to implement my own take on Discord's flavor of markdown into my web application. The way I've done so is simply by chaining replace methods, each checking and replacing the syntax with proper HTML tags (I do sanitize, don't worry).
let description = description.replace(/\`{3}([\S\s]*?)\`{3}/g, '<code>$1</code>')
.replace(/\`(.*)\`/g, '<code class="inline">$1</code>')
.replace(/~~([\S\s]*?)~~/g, '<s>$1</s>')
The problem I'm facing is that the regex also matches inside of entire code blocks and also inside inline code. This behavior is not wanted.
**bold and
*italic and
__underline and
~~strikethrough~~__***
`~~Not strikethrough~~`
~~`Strikethrough`~~
Normal text
```
~~Not strikethrough~~
```
~~```
Strikethrough
```~~
**bold and
*italic and
__underline and
~~strikethrough~~__***
`~~Not strikethrough~~`
~~`Strikethrough`~~
Normal text
I've tried something like this: /(?<!`[\S\s])\*([\S\s]*?)\*(?!`)/g
but I can't get it to work like expected.
I'm still learning regex and continue to find it hard to wrap my head around, so any and all help is much appreciated.
Jan. 4. 2021
Sorry I didn't clarify earlier but the stylings should be "nestable", or in other words able to be combined e.g. ***strong and italic*** should become strong and italic
I've updated the input text (see above) to better encapsulate all probable use cases.