Escaping Discord subset of markdown
<p>I'm trying to escape the subset of markdown that Discord supports (<code>*</code>, <code>_</code>, <code>`</code>, <code>~</code>).
Characters that are already escaped should not have additional backslashes added.
This is what I have:</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>function escapeMarkdown(text) {
 return text.replace(/([^\\]|^|\*|_|`|~)(\*|_|`|~)/g, '$1\\$2');
}

console.log(escapeMarkdown('*test* _string_ ~please~ `ignore` *_`~kthx \*  \\~'));</code></pre>
</div>
</div>
</p>

<p>This works fine, minus the fact that multiple markdown characters against each other will not all be escaped. I'm not sure how to expand this to allow for that, without making the expression absurdly complicated.</p>