MathJax + Markdown rendering in comments

The following comment (edited down from a real-world example) "$\cong$—spacer *italics* spacer—$\mapsto$" should render roughly as "≅—spacer italics spacer—↦", but, on MathJax-enabled sites like MathOverflow, the stars render literally, rather than entering italics. This appears only to happen in comments, not in post bodies.

I made a Meta.MO post about this, and @MartinSleziak suggested that it might be appropriate to mention it here.

EDIT: See further exploration in a comment by @j.c., which I reproduce here as source:

Here's a slightly more minimal example: $\cong— *italics* \cong$: code
$\cong— *italics*$\cong$. The lack of a space between the MathJax and em-dash is necessary ($\cong$— *italics*$\cong$: code $\cong$— *italics*$\cong$), and there also must be MathJax on both sides ($\cong$— *italics*: code $\cong$— *italics*).  • The code used to hook MathJax into the Markdown for the page is designed to match the way that MathJax locates the math within the page (and it removes the math, then runs Markdown, then puts the math back). I suspect that the code for the comments has not been so carefully crafted. Indeed, I suspect that they are simply using a regular expression match like /\$(.*)\$/ to locate the math, and so this would match the first dollar sign to the last one, and consider the entire contents to the math (including the stars), preventing it from being processed for the mini-Markdown in comments. – Davide Cervone Jul 22 '18 at 15:02 • Using /\$(.*?)\\$/` would do better, but still would not always work. It turns out that math that is renderable by MathJax can't be identified by a standard regular expression. – Davide Cervone Jul 22 '18 at 15:03
• @DavidCervone The explanation is not so simple. In particular, everything works correctly when the em-dashes are removed, even though the Markdown markup is still placed between two MathJax fragments. – Emil Jeřábek Jul 22 '18 at 19:41
• @EmilJeřábek, thanks for the extra details. I didn't experiment with it myself. I only suggest that the algorithm used by the comment sections is probably matching the wrong dollar signs. It may be that they are using some idea of word-breaks before the initial and after the final dollar signs, and that the em-dash is making the second dollar signs appear to not be a final one (i.e., not followed by a proper word break character). – Davide Cervone Jul 22 '18 at 23:38