9
qAnswersR[90430] = [];
    qAnswersR[90430].push("[math]k: \frac{(x+20)^{2}}{256}+\frac{(y-15)^{2}}{81}=1[/math]");

And I need to get the value into variable, but when I console.log out the array like this:

console.log(qAnswersR[90430]);

I get: [math]k: rac{(x+20)^{2}}{256}+rac{(y-15)^{2}}{81}=1[/math],[math]k: 81(x+20)^{2}+256(y-15)^{2}=20736[/math]

But the escape tag "\" disappears, but I need it there, what should I do?

codemania
  • 1,098
  • 1
  • 9
  • 26
Mike
  • 6,854
  • 17
  • 53
  • 68

3 Answers3

6

You can use tagged template literals

var str = (s => s.raw)`[math]k: \frac{(x+20)^{2}}{256}+\frac{(y-15)^{2}}{81}=1[/math]`[0]

The anonymous arrow function will serve as tag and s.raw contains the original input

5

But the escape tag "\" disappears, but I need it there, what should I do?

You need to escape the backslash, i.e., use \\ instead of just \:

"[math]k: \\frac{(x+20)^{2}}{256}+\\frac{(y-15)^{2}}{81}=1[/math]"
          ^                       ^
aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 1
    Ok thx, what do you think would be the best function in php to do that? since the script is generated in php. (addslashes?) – Mike May 31 '11 at 07:49
  • Have a look at this question: http://stackoverflow.com/questions/168214/pass-a-php-string-to-a-javascript-variable-including-escaping-newlines – aioobe May 31 '11 at 07:58
3

Escape the escape character, like \\a.

alex
  • 479,566
  • 201
  • 878
  • 984