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).
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.
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: \\ .
"/\\" is the JavaScript representation of forwardslash backslash, just as you intend.
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.
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/'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With