JS: String.raw

By Xah Lee. Date: . Last updated: .

String.raw

(new in ECMAScript 2015)

String.rawtemplateString

return the string but leave the String Escape Sequence as is.

console.log(String.raw`\n` === "\\n" );
// true
console.log(String.raw`\n`);
// \n

// prints literally backslash n
// embeded expressions are still evaluated
console.log(String.raw`${3 + 1}\n${3 + 2}`);
// 4\n5

// without string raw
console.log(`${3 + 1}\n${3 + 2}`);
// 4
// 5

Common Use Cases

Regular Expressions

// replace digits by x.

console.assert("5826".replace(RegExp( String.raw`\d+`), "x") === "x");

Windows file paths

const path = String.raw`C:\Users\John\Documents\file.txt`;

JavaScript. String