--- title: String.raw() slug: Web/JavaScript/Reference/Global_Objects/String/raw translation_of: Web/JavaScript/Reference/Global_Objects/String/raw original_slug: Web/JavaScript/Referencia/Objetos_globales/String/raw ---
El método estatico String.raw()
es una función de plantilla de literales, similar al prefijo r
en Python o al prefijo @
en C# para strings literales (con ciertas diferencias: ver la explicación en este problema). Se utiliza para obtener un string crudo a partir de plantillas de string (es decir, el original, texto no interpretado).
String.raw(callSite, ...substitutions)
String.raw`templateString`
callSite
{ raw: ['foo', 'bar', 'baz'] }
....substitutions
templateString
${...}
).La forma cruda del string de una plantilla string proporcionada.
En la mayoría de los casos, String.raw()
es usado con plantillas string. La primera sintaxis mencionada arriba es raramente usada, porque el motor de JavaScript hará la llamada por ti con los argumentos apropiados, al igual que otras funciones de etiqueta.
String.raw()
es la unica función de etiqueta incorporada en las plantillas string; trabaja igual que la función de la plantilla por defecto y ejecuta la concatenación. Incluso puedes reimplementarlo con código normal de JavaScript.
String.raw()
String.raw`Hi\n${2+3}!`; // 'Hi\n5!', the character after 'Hi' // is not a newline character, // '\' and 'n' are two characters. String.raw`Hi\u000A!`; // 'Hi\u000A!', same here, this time we will get the // \, u, 0, 0, 0, A, 6 characters. // All kinds of escape characters will be ineffective // and backslashes will be present in the output string. // You can confirm this by checking the .length property // of the string. let name = 'Bob'; String.raw`Hi\n${name}!`; // 'Hi\nBob!', substitutions are processed. // Normally you would not call String.raw() as a function, // but to simulate `t${0}e${1}s${2}t` you can do: String.raw({ raw: 'test' }, 0, 1, 2); // 't0e1s2t' // Note that 'test', a string, is an array-like object // The following is equivalent to // `foo${2 + 3}bar${'Java' + 'Script'}baz` String.raw({ raw: ['foo', 'bar', 'baz'] }, 2 + 3, 'Java' + 'Script'); // 'foo5barJavaScriptbaz'
Especificación | Estado | Comentario |
---|---|---|
{{SpecName('ES2015', '#sec-string.raw', 'String.raw')}} | {{Spec2('ES2015')}} | Definicion inicial. |
{{SpecName('ESDraft', '#sec-string.raw', 'String.raw')}} | {{Spec2('ESDraft')}} |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
{{Compat("javascript.builtins.String.raw")}}