admin 管理员组

文章数量: 1086019

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 ';chl=' + tex;
}

However calling getLatexImage('\frac{a}{b}') gives me: ";chl= rac{a}{b}"

\f is being ignored.

Any suggestions?

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./chart?cht=tx&chl=' + tex;
}

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

\f is being ignored.

Any suggestions?

Share Improve this question asked Sep 7, 2015 at 8:41 WagmWagm 3686 silver badges13 bronze badges 3
  • 1 escape the symbol -put '\\' instead of '\' – Ivaylo Strandjev Commented Sep 7, 2015 at 8:42
  • See msdn.microsoft./library/2yfce773(v=vs.94).aspx – Tushar Commented Sep 7, 2015 at 8:42
  • Pretty sure you could have used search to find this question... – akalikin Commented Sep 7, 2015 at 8:49
Add a ment  | 

4 Answers 4

Reset to default 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'); 

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

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 \

You can simply escape it by adding another backslash

 '\\b'

本文标签: Javascript strings with symbol 3939Stack Overflow