开发者

How to make javascript ignore escape ( \ ) character? [duplicate]

开发者 https://www.devze.com 2023-03-09 04:11 出处:网络
This question already has answers here: How can I use backslashes (\\) in a string? (4 answers) Closed 3 years ago.
This question already has answers here: How can I use backslashes (\) in a string? (4 answers) Closed 3 years ago.
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]开发者_如何学C

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


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]"
          ^                       ^


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


Escape the escape character, like \\a.

0

精彩评论

暂无评论...
验证码 换一张
取 消