javascript 如何让javascript忽略转义(\)字符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6184453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to make javascript ignore escape ( \ ) character?
提问by Mike
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 像这样注销数组时:
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]
我得到: [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?
但是转义标签"\"
消失了,但我需要它,我该怎么办?
采纳答案by aioobe
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]"
^ ^
回答by alex
Escape the escape character, like \\a
.
转义转义字符,例如\\a
.
回答by Alexander Praetorius
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
匿名箭头函数将作为标签并s.raw
包含原始输入