How to pass string with backslashes from backend?

Hi Guys,

Need some advice/help.

I am working on displaying complicate Math equations using MathJax.

A sample of the HTML contents I am testing can be seen here: (Note: copy whole file to .html, replace the line " " with “” and open it with any latest browser)
https://github.com/mathjax/MathJax/blob/master/test/sample.html

Scenario 1. When I hardcode the following line inside the “code” of a “HTML” object with the same “script” line above, it works.

\[P(E) = {n \choose k} p^k (1-p)^{ n-k} \]

Scenario 2. When I follow the HTML object sample and pass the string from page code using “postMessage” it also works, except I have to replace all single backslashes with double backslashes as shown below.

\\[P(E) = {n \\choose k} p^k (1-p)^{ n-k} \\]

Scenario 3 is where the problem is. To hide all the logics that generate the equation, I want to pass the MathJax equation string from backend module. But as the backslash is some sort of control character, couldn’t work out correct way to pass any string containing either single/double/triple backslashs, then pass it to the same HTML object through same “postMessage” as before, and display the math equation properly in the HTML object.

Any idea/suggestion?