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.

embeded expressions ${expr} are still evaluated.

console.assert(
 String.raw`\n` === "\\n",
);
console.assert(
 String.raw`\n` === "\\n",
);
// embeded expressions are still evaluated
console.assert(
 String.raw`${3 + 1}\n${3 + 2}` === "4\\n5",
);

Common use cases

Regular Expression

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

Windows file path

const path = String.raw`C:\Users\Joe\Documents\x.txt`;