-2

Declaring

text = '{"section": "\main" }'

results in

"{"section": "main" }"

Is there a way to prevent the Javascript interpreter from treating the backslash as an escape character and removing it?

I want to be able to declare "{"section": "\main" }" and keep the backslash in the output"

"{"section": "\main" }"

Note: I realise that if I use two backslashes it will give me the desired output. But I don't want to use two backslashes as I have a lot of Latex code that I am including in a webpage and it is extremely awkward having to double escape everything. So is there a way to prevent Javascript escaping text?

8
  • 4
    Use a text editor to replace "\" with "\\" in your Latex code, it's not awkward at all. Commented Aug 15, 2016 at 15:08
  • Have the text in a div and read the div's innerText into a var if you do not want to escape @Mikey - yes I saw it wand removed my comment Commented Aug 15, 2016 at 15:09
  • As far as I know, JS can not do this. Commented Aug 15, 2016 at 15:11
  • Also called back tics in coding Commented Aug 15, 2016 at 15:38
  • @dandavis are you sure it's enough? From my tests in both FF and chrome, `\main` is escaped just like "\main" Commented Aug 15, 2016 at 15:52

3 Answers 3

6

In supporting browsers, you could use the String.raw method like this :

console.log(String.raw`\main`);

Sign up to request clarification or add additional context in comments.

5 Comments

Are the back tics on their own enough? Cannot test this right now
@mplungjan good question, also saw this comment and on my way back to the keyboard to test this after dandavis comment.
@mplungjan So the answer is nope in FF : 'hello'+`\main`->"hellomain", while 'hello'+String.raw`\main`⁣⁣⁣ correctly outputs "hello\main". (ps: funny how I had to use backslashes in here to make ths comment ;-) )
The data is actually in a variable. Is it still possible to use String.raw with a variable instead of direct text?
@csss, as said by mplungjan on his answer's comment, "Then [your data is] already escaped!". If it's not, then JSON parser will throw you an SyntaxError : "bad escaped character ..."
1

console.log(document.getElementById("latextstrings").innerText)
<div id="latextstrings" style="display:none">
  {"section": "\main" }
  {"section": "\sub" }</div>

6 Comments

How are you gonna set the innerHTML?
So non dynamic. Probably fine to OP, even if then I would probably myself prefer a comment node than an hidden element.
I have a lot of Latex code that I am including in a webpage
Yep "probably fine to OP" I said, just before upvote. But his question is how to make js not escape text, so this is a server-side workaround, not a js one.
Sure but I read it as "how to make unescaped latex strings available to JavaScript" for later use
|
-1

Just double-escape it, it's the simpliest

"\\main"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.