Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 484
Mike Avatar asked May 31 '11 07:05

Mike


People also ask

How do I ignore an escape character in a string?

An escape sequence is a set of characters used in string literals that have a special meaning, such as a new line, a new page, or a tab. For example, the escape sequence \n represents a new line character. To ignore an escape sequence in your search, prepend a backslash character to the escape sequence.

How do I ignore JavaScript?

The ignoreCase property specifies whether or not the "i" modifier is set. This property returns true if the "i" modifier is set, otherwise it returns false.

How do I stop escape characters in HTML?

Using the character encoding UTF-8 for your page means that you can avoid the need for most escapes and just work with characters.


3 Answers

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]"
          ^                       ^
like image 180
aioobe Avatar answered Sep 17 '22 22:09

aioobe


Escape the escape character, like \\a.

like image 44
alex Avatar answered Sep 18 '22 22:09

alex


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

like image 41
Alexander Praetorius Avatar answered Sep 20 '22 22:09

Alexander Praetorius