1

I am having difficulties handling symbol \ in javascript. Strings seems to ignore it, for example alert('a\b') would only alert a.

My goal is to write following function which would give me latex image:

function getLatexImage(tex)
{
    return 'http://chart.apis.google.com/chart?cht=tx&chl=' + tex;
}

However calling getLatexImage('\frac{a}{b}') gives me: "http://chart.apis.google.com/chart?cht=tx&chl= rac{a}{b}"

\f is being ignored.

Any suggestions?

3
  • 1
    escape the symbol -put '\\' instead of '\' Commented Sep 7, 2015 at 8:42
  • See msdn.microsoft.com/library/2yfce773(v=vs.94).aspx Commented Sep 7, 2015 at 8:42
  • Pretty sure you could have used search to find this question... Commented Sep 7, 2015 at 8:49

4 Answers 4

4

\ is an escape character. It starts an escape sequence.

\n is a new line. \t is a tab. An escape sequence that has no special meaning usually gets turned into the character on the RHS (so \b is b).

To have a backslash as data in a string literal you have to escape it:

alert('a\\b'); 
Sign up to request clarification or add additional context in comments.

Comments

3

Use \\. Single slashes are used for special signs (like \n, \t..)

Comments

2

The backslash \ is a escape character in JavaScript and many other programming languages. If you want to output it, you'll need to escape it by itself.

\\a

for example would output \a.

That being said, if you want to use it in an url you should encode it for safety.

%5c

translates to \

1 Comment

Or go over the top like me and url encode everything! +1 for mentioning that.
2

You can simply escape it by adding another backslash

 '\\b'

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.