--- title: String.raw() slug: Web/JavaScript/Reference/Global_Objects/String/raw tags: - ECMAScript 2015 - JavaScript - Method - Reference - String - Polyfill translation_of: Web/JavaScript/Reference/Global_Objects/String/raw ---
String.raw()
메서드는 템플릿 리터럴의 태그 함수입니다. 이는 Pyhon의 r
접두사 또는
C#의 문자열 리터럴의
@
접두사와 유사합니다. (그러나 동일 하지는 않습니다. 이 문제에 관해서는 여기 이슈를 참조하십시오.) 이 메서드는 템플릿 리터럴의 윈시 문자열을 가져오는데
사용됩니다.
즉, 대체(예: ${foo}
)는 처리되지만 이스케이프(예: \n
)는 처리되지 않습니다.
String.raw(callSite, ...substitutions) String.raw`templateString`
callSite
{ raw: ['foo', 'bar', 'baz'] }
.
...substitutions
templateString
${...}
).주어진 템플릿 리터럴의 원시 문자열 형식.
String.raw()
의 경우 보통 템플릿 리터럴과 많이 사용하고, 첫번째 구문의 경우 잘 사용되지 않습니다.
왜냐하면 자바스크립트 엔진이 당신을 위해서 자동으로 적절한 인수로 호출해주기 때문입니다. ( 다른 태그 메서드들과 마찬가지로).
String.raw()
은 템플릿 리터럴의 유일한 내장 함수입니다. 기본 템플릿 기능과 동일하게 작동하며 연결을 수행합니다. JavaScript 코드를 사용하여 다시 구현할 수도 있습니다.
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 `foo${2 + 3}bar${'Java' + 'Script'}baz` you can do: String.raw({ raw: ['foo', 'bar', 'baz'] }, 2 + 3, 'Java' + 'Script'); // 'foo5barJavaScriptbaz' // Notice the first argument is an object with a 'raw' property, // whose value is an iterable representing the separated strings // in the template literal. // The rest of the arguments are the substitutions. // The first argument’s 'raw' value can be any iterable, even a string! // For example, 'test' is treated as ['t', 'e', 's', 't']. // The following is equivalent to // `t${0}e${1}s${2}t`: String.raw({ raw: 'test' }, 0, 1, 2); // 't0e1s2t'
{{Compat}}
String.raw
의 폴리필은 여기를 참고core-js