Sub topic 6
String.raw: The Built-In Tag
This chapter assumes you know how a tag function receives a strings array and its values. It also assumes you know that each string part has a cooked form, strings[i], and a raw form, strings.raw[i]. String.raw is the one tag that JavaScript ships with. It joins the raw parts with the substitution values, so backslashes stay exactly as you typed them.
What String.raw Does
String.raw works like the interleaving tags from earlier chapters. The difference is that it reads strings.raw instead of the cooked strings.
const path = String.raw`C:\temp\new`;
console.log(path); // C:\temp\new
console.log(path.length); // 11
const cooked = `C:\temp\new`;
console.log(cooked.length); // 9: \t became a tab, \n became a newlineSubstitutions are still evaluated as normal JavaScript expressions. Only the static text between them is kept raw.
const dir = 'logs';
console.log(String.raw`C:\${dir}\today`); // C:\${dir}\today
console.log(String.raw`C:\${''}${dir}\today`); // C:\logs\todayIn the first line, \$ stops the substitution from being recognized. The raw text keeps the backslash, so you get a literal \${dir}. If you want a backslash right before a substitution, the second line shows one workaround.
Common Uses
Raw strings are useful anywhere backslashes carry meaning of their own:
| Use | Example |
|---|---|
| Windows paths | ``String.raw`C:\Users\ada` `` |
| Regular expression source | ``new RegExp(String.raw`\d+\.\d+`)`` |
| LaTeX snippets | ``String.raw`\frac{1}{2}` `` |
The regex case removes a common source of bugs. With a normal string, '\d' loses its backslash, so you have to write '\\d'. With String.raw you write the pattern the way it appears in a regex literal.
const version = new RegExp(String.raw`^v(\d+)\.(\d+)$`);
console.log(version.test('v2.14')); // trueCalling String.raw as a Plain Function
String.raw is an ordinary function. You can call it directly with any object that has a raw property holding an array-like value. A string counts as array-like, and each of its characters becomes one part.
console.log(String.raw({ raw: ['a', 'b', 'c'] }, 1, 2)); // a1b2c
console.log(String.raw({ raw: 'abc' }, '-', '-')); // a-b-c
console.log(String.raw({ raw: ['x', 'y', 'z'] }, 1)); // x1yz
console.log(String.raw({ raw: ['x', 'y'] }, 1, 2, 3)); // x1yThe last two lines show the edge rules. If there are too few values, the missing ones count as empty strings. If there are too many, the extras are ignored. The raw parts decide how long the output is.
Gotchas
- A trailing backslash is a syntax error. In `
String.rawC:\, the\`escapes the closing backtick, so the literal never ends. Use'C:\\'or put the backslash in a substitution:String.rawC:${'\\'}`. - Escaped backticks keep their backslash. `
String.rawa\b`producesa\b `, notab ``. - Values are converted with
String(). Objects become[object Object]unless they definetoString. - The output is not escaped in any way.
String.rawdoes not make a string safe for HTML or SQL.
Exercise
What does String.raw\n${1 + 1}\t evaluate to, and what is its length?
Show answer
The four-character string \n2\t is wrong; the result is the five-character sequence backslash, n, 2, backslash, t. Its length is 5. The static parts stay raw, and the substitution is evaluated to 2.
Exercise
What does String.raw({ raw: 'hey' }, 0) return?
Show answer
'h0ey'. The string 'hey' is treated as the parts 'h', 'e', 'y'. The one value 0 goes after 'h', and the missing second value counts as an empty string.