How to make javascript ignore escape ( \ ) character?

家住魔仙堡 提交于 2020-01-03 08:42:06

问题


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?


回答1:


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



回答2:


Escape the escape character, like \\a.




回答3:


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



来源:https://stackoverflow.com/questions/6184453/how-to-make-javascript-ignore-escape-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!