Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Backslash '\' in console.log() not appearing

I'm trying to use a back slash in console.log() and within <p></p> but it seems that when the page loads, all back slashes are removed.

Example JS

console.log('\m/ Lets rock. \m/');

Result

m/ Lets rock. m/

How can I prevent it from being removed?

EDIT: Backslash not forward slash. Running this on node.js with express, within the <head> tags of layout.jade. Backslash visible in REPL, but not when running on node in the web browser (Chrome & Firefox).

like image 312
Nyxynyx Avatar asked Dec 02 '11 03:12

Nyxynyx


People also ask

How do you show backslash in string?

If you want to include a backslash character itself, you need two backslashes or use the @ verbatim string: var s = "\\Tasks"; // or var s = @"\Tasks"; Read the MSDN documentation/C# Specification which discusses the characters that are escaped using the backslash character and the use of the verbatim string literal.

How do you add a backslash in JavaScript?

In JavaScript, the backslash has special meaning both in string literals and in regular expressions. If you want an actual backslash in the string or regex, you have to write two: \\ .

How do I print a backslash in typescript?

"/\\" is the JavaScript representation of forwardslash backslash, just as you intend.

What does console log () do?

The console. log() method outputs a message to the web console. The message may be a single string (with optional substitution values), or it may be any one or more JavaScript objects.


1 Answers

If m/ Lets rock. m/ is your result, I see forward slashes.

If you mean backslashes, escape them to show that the string wants a literal backslash:

console.log('\\m/ Lets rock. \\m/');

Otherwise, JavaScript interprets this as a \m EscapeSequence. That's why you need the \\ EscapeSequence.


The solution was that the backslashes needed to be double escaped:

console.log('\\\\m/ Lets rock. \\\\m/');

...apparently the backslashes are processed as escaped characters twice (once in the initial string creation, then again for some other purpose).

So the string creation gives us:

'\\m/ Lets rock. \\m/'

...then the subsequent processing results in:

'\m/ Lets rock. \m/'
like image 171
RightSaidFred Avatar answered Oct 20 '22 04:10

RightSaidFred