Movatterモバイル変換


[0]ホーム

URL:


  1. Tecnología web para desarrolladores
  2. JavaScript
  3. Referencia de JavaScript
  4. Objetos globales
  5. String — Cadena de caracteres
  6. String.raw()

Esta página ha sido traducida del inglés por la comunidad.Aprende más y únete a la comunidad de MDN Web Docs.

View in EnglishAlways switch to English

String.raw()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨septiembre de 2015⁩.

El método estaticoString.raw() es una función deplantilla de literales, similar al prefijor en Python o al prefijo@ en C# para strings literales (con ciertas diferencias: ver la explicación eneste problema). Se utiliza para obtener unstring crudo a partir de plantillas destring (es decir, el original, texto no interpretado).

Sintaxis

String.raw(callSite, ...substitutions)String.raw`templateString`

Parametros

callSite

Plantilla bien estructurada, similar a{ raw: ['foo', 'bar', 'baz'] }.

...substitutions

Contiene valores de sustitución.

templateString

[opcional] Unaplantillastring, con sustituciones (${...}).

Valor de regreso

La forma cruda delstring de una plantillastring proporcionada.

Excepciones

TypeError

UnTypeError es arrojado si el primer argumento no es un objeto bien estructurado.

Descripción

En la mayoría de los casos,String.raw() es usado con plantillasstring. 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 otrasfunciones de etiqueta.

String.raw() es la unica función de etiqueta incorporada en las plantillasstring; 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.

Ejemplos

UsandoString.raw()

js
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'

Especificaciones

Specification
ECMAScript® 2026 Language Specification
# sec-string.raw

Compatibilidad con navegadores

Tambien ver

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp