I am trying to write code to generate a LaTeX table using JavaScript. The problem is that I can’t find a way to escape ampersand characters in the resulting LaTeX. The result has either been an unescaped ampersand or an ampersand with 2 backslashes. This is part of a website and is on the client side, so it would be preferable not to have to use an NPM package.
I tried using replace and replaceAll. When I use replace like string.replace("&", "&")
, it does not change anything with the string. When I use replace like string.replace("&", "\&")
, it replaces the ampersand with “&” (a string with 2 backslashes). What I want is for it to replace the ampersand with “&”, aka a string with 1 backslash.