Special characters are not displayed by the console.log

Asked

Viewed 94 times

4

I’m solving a school exercise and I need to print a string with special characters using console.log, however the \ contained in the string are removed when executing the code.

In search of the exercise resolution, I searched for special Javascript characters and was conditioned to search for regular expressions. Finding only commands that limit the use of characters, and not allow them.

So how do I make it to the console.log display the \?

Follow code I wrote firsthand:

var myString = "If There Is Bread Winners There Is Bread Losers. But You Can\'t Toast What Isn\'t Real."

console.log(myString)

2 answers

6


The character \ is not "removed from the string". Actually it is part of a escape sequence.

Basically, when you create a string, you have to put its contents in quotes:

var string = 'conteúdo da string';

But what if I want to put the quotes themselves? This doesn’t work here:

var string = 'conteúdo 'da' string'; // errado!

For seeing the ' before da, Javascript understands that you are actually closing the quotes, and therefore terminating the content of the string. In this case, we need to use the escape sequence \' to represent the character ':

var string = 'conteúdo \'da\' string'; // certo

That is, the sequence \' is interpreted as the character ', indicating that it is part of the content of the string and should not be treated as the quotes that delimit the string itself. So much so that if you print this string, the result will be conteúdo 'da' string.

But then we have another problem: whether the \ is used for escape sequences, as do to represent the character itself \? Simple, with another escape sequence (in case, it would be \\):

console.log('can\'t'); // can't
console.log('can\\t'); // can\t

In the code above, \' is the escape sequence representing the character ', so the resulting string is can't. Already \\ is the escape sequence representing the character \, so the result is can\t.

If you want the resulting string to be can\'t, then you’ll have to escape both:

console.log('can\\\'t'); // can\'t

I mean, we have \\ (that represents the character \) followed by \' (that represents the character '), resulting in can\'t.


Remember also that if you use double quotes (") to delimit the string, there is no need to escape the ' (but the \ still need):

// o ' está com escape
console.log("can\'t");  // can't
// mas dentro de aspas duplas, não precisa
console.log("can't");   // can't

// mas o \ precisa
console.log("can\\'t"); // can\'t


Finally, regex has nothing to do with it. Regex would be used if you wanted to detect such characters and/or remove them from the string, for example (still, for simpler cases or need regex). But if it is to generate a string with such characters, just understand how escape sequences work.


See also: Why the expression ``` == '`' is true?

4

An alternative would be to use the static method String.raw() as a tag function for a literal template containing your text:

var myString = String.raw`If There Is Bread Winners There Is Bread Losers. But You Can\'t Toast What Isn\'t Real.`

console.log(myString)

The documentation says that as a tag String.raw() is similar to the prefix r in Python or the prefix @ in C# for literal string. Where it is used to take strings in the "raw" format of a literal template, where substitutions are processed, but escape sequences are not.

There is also other cases of use of String.raw() beyond the scope of the question.

Documentation of the method alert for a bug that happened on Chrome and see in its compatibility table that so far the method is not supported by IE and Opera browsers.

//Situações que geravam bug no passado.
console.log(String.raw `\x5c`);
console.log(String.raw `с:\x.js`);

Another possibility is also to create the own tag function returning the special property raw, available in the first function argument:

function r(strings, ...values) {
  return strings.raw[0];
}

var myString = r`If There Is Bread Winners There Is Bread Losers. But You Can\'t Toast What Isn\'t Real.`

console.log(myString)

That approach is more compatible with modern browsers just not working in IE.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.