What is String.raw for in Javascript?

Asked

Viewed 619 times

9

Well, that’s the question.

I would like to know what the method is for String.raw in Javascript.

I saw something in the documentation of MDN, but I did not understand very well the use

Has something to do with string templates?

3 answers

11


The String.raw serves to avoid escaping strings like:

  • \n
  • \r
  • \0
  • \x01
  • \u000A

That is, if I do this:

console.log(`foo\nbar`);

It will show a line break, but if you do this:

console.log(String.raw`foo\nbar`);

Will show exactly \n, I believe that the main utility of this is to send via JSON, by requests (HTTP or not) without escaping, since in JSON who usually treats this is the interpreter at the time of "reading".

Is similar to r used in Python, as for example in Python2:

#string escapada
print 'Foo\nbar'
 
#string literal
print r'Boo\nbaz'

This will be displayed:

Foo
bar
Boo\nbar

Or as the @ in C#:

//string escapada
Console.WriteLine("Foo\nbar");
 
//string literal
Console.WriteLine(@"Boo\nbaz");

Answering the specific question:

Has some relationship with string templates?

It is related, because the String.raw only works with string templates, with string, it will not work with quotes "..." or single quotes '...', only with the grave accent, which in Javascript is the string template:

`....`

As already very well answered by @bfavareto in the question below, this is an ES6 implementation:

  • @dvd o I believe there has nothing to do with the API and syntax, but rather with the data transport, and as I said, I believe it is the main reason about this and just about this, the rest of the answers and no references have nothing to do with, "I think," "I believe," "I believe",therefore it is about a specific thing and a specific point of the answer that does not change at all the rest of the statement ;)

  • 1

    Pointing nazi Grammar: the name of the crase accent is accent severe. Crase is the name of the contraction that happens when you join the preposition "a" with another word beginning in "a". https://www.normaculta.com.br/palavras-com-acento-grave/ =)

  • @nunks as you can see where this written CRASE is an automatically generated title from the linked question, so it was not I who wrote CRASE, was the author of the other question ;)

  • @nunks edited the linked question and corrected the title.

  • @nunks really, read better and I had written crase accent, thank you!

7

The method String.raw is an implementation of the 6th edition of Ecmascript (ECMA-262) and serves to convert string templates (literals template) plain text.

According to the mentioned documentation, it converts a string into an unenterpreted literal string, that is, everything inside the string will be considered in plain text, ignoring interpretatible passages (tags, escapes, etc.) but keeping the format of the string exactly as you wrote in the code (break lines, spaces, tabs etc.).

Example:

const template = String.raw`Primeira linha
Segunda linha
Tags <b>são ingnoradas</b>
ou qualquer tipo de interpretação,
como este escape -> \n
  <- inclusive este espaço TAB à esquerda
ou este espaço enorme ->        <-
`;
console.log(template);

5

The method String.raw is used to get the string clean without escaping the characters. eg:

console.log("String com caracteres \nespeciais");
console.log(String.raw`String com caracteres especiais\n`);

Output:

String com caracteres
especiais
String com caracteres especiais\n

It can be useful in string templates when you want to put a regex expression inside it without escaping the character "\".

Browser other questions tagged

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