String.raw Preserves Backslashes In Templates

Build Windows paths and regex patterns with String.raw tagged templates, comparing raw backslashes against normal escapes.

JavascriptData Integrity

Code

javascriptsnippet
18 lines
// String.raw tagged templates keep backslash escapes literal
const windowsPath = String.raw`C:\temp\new\file.txt`;
console.log('path:', windowsPath);
console.log('contains backslash-n:', windowsPath.includes('\\n'));

const pattern = String.raw`\d+\.\d+`;
console.log('pattern:', pattern);
const regex = new RegExp(pattern);
console.log('matches 3.14:', regex.test('3.14'));

const normal = `C:\temp\new`;
console.log('normal length:', normal.length);
console.log('raw length:', windowsPath.length);
console.log('normal has tab:', normal.includes('\t'));

const joined = String.raw({ raw: ['a', 'b', 'c'] }, '-', '_');
console.log('manual raw call:', joined);
console.log('backslash count:', windowsPath.split('\\').length - 1);

What the code does

Top-level bindings windowsPath, pattern, regex, normal hold the demo state — nothing sits inside a function scope.

Using it

The run emits 9 labeled sections — path:, contains backslash-n:, pattern:, matches 3.14:, normal length:, raw length:, normal has tab:, manual raw call:, backslash count: — one per step.

Key statements

const joined = String.raw({ raw: ['a', 'b', 'c'] }, '-', '_');
const windowsPath = String.raw`C:\temp\new\file.txt`;
const pattern = String.raw`\d+\.\d+`;

Interactive Tools for This Topic

FAQ

Are there any imports to resolve outside the language?
No. Every import resolves against the language's own modules. Its state lives in module-level bindings: `windowsPath`, `pattern`, `regex`.