9

String.raw can be used to create a string that contains backslashes, without having to double up those backslashes.

Historically, you'd need to double up backslashes when creating a string:

let str = "C:\\Program Files\\7-Zip";
console.log(str);

String.raw allows your code to show the path without doubled backslashes:

let str = String.raw`C:\Program Files\7-Zip`;
console.log(str);

The above code works fine, but today I discovered that it doesn't work if the raw string ends with a backslash:

let str = String.raw`Can't End Raw With Backslash\`;
console.log(str);

The above snippet produces this error:

{
  "message": "SyntaxError: `` literal not terminated before end of script",
  "filename": "https://stacksnippets.net/js",
  "lineno": 14,
  "colno": 4
}

Why is this an exception?

Update: Another example where the single backslash doesn't do what I'd like is:

let str1 = "folder";
let str2 = "subfolder";
let fileName = "file.txt"
let path = String.raw`\${str1}\${str2}\${fileName}`;
console.log(path);

While I was hoping for this output:

\folder\subfolder\file.txt

Instead it outputs:

\${str1}\${str2}\${fileName}

The backslash escapes the dollar sign's special meaning. So, this is yet another circumstance where you still have to double up those backslashes :(

Lonnie Best
  • 9,936
  • 10
  • 57
  • 97
  • Today I had a similar situation in python. [Python's implementation of raw strings](https://docs.python.org/3/reference/lexical_analysis.html#literals) is better (for this use-case) than [EcmaScript's](https://tc39.es/ecma262/multipage/text-processing.html#sec-string.raw) in my opinion. – Lonnie Best Jul 30 '22 at 11:04

2 Answers2

5

It can, but remember that there's the "literal" character and the backslash character. You're asking for a literal backtick. Ask for a literal backslash:

let str = String.raw`...\\`;

Any character immediately following a backslash is treated as its literal version, regardless of what it is. String.raw can work around some of those limitations, but not all. It suppresses interpolation of things like \n but can't prevent you from accidentally adding a literal backtick.

tadman
  • 208,517
  • 23
  • 234
  • 262
  • I guess this is low-level parser baggage that `String.raw` couldn't overcome. Thanks for the explanation. – Lonnie Best Apr 24 '20 at 20:36
  • I think it's intentional. How else could you put a backtick in the string? – tadman Apr 24 '20 at 20:36
  • 2
    Yeah, I see what you mean. It's a shame; until now, I thought I had found the perfect solution for not having to double up those backslashes. Too bad it comes with one gotcha. – Lonnie Best Apr 24 '20 at 20:58
  • 3
    This doesn't seem like a correct solution. `String.raw\`...\\\`` gives me `"...\\\\"`, not `"...\\"`. – jtbandes Nov 01 '21 at 16:12
  • 1
    @jtbandes You're misreading the output. When inside of double quotes the singular backslash must be escaped. Check with `console.log("...\\\\")`. If this was incorrect you'd actually see `"...\\\\\\\\"`. The string `"...\\"` actually contains a single backslash character. – tadman Nov 04 '21 at 07:58
  • 2
    I skipped a logical step in my previous comment — the OP's question was about how to create a string that ends with a **single** backslash. Your answer does show that `String.raw` can be used to create a string that ends with **two** backslashes, but at first glance it looks like you're saying that a trailing backslash can be escaped as \\, so `String.raw\`...\\\`` will create a string ending with one backslash. But it doesn't, and in fact there doesn't seem to be any way to do it with String.raw as far as I can tell... except perhaps `String.raw\`...${"\\"}\`` – jtbandes Nov 04 '21 at 15:10
3

Ending \` is always parsed as escaped ` symbol, meaning there's no matching closing backtick for a template literal. This makes it less consistent regarding how backslash can be used with String.raw.

For more special cases a string can be padded per need basis with characters that aren't expected in resulting sting.

It can be a newline:

const trimmedLine = (...args) => String.raw(...args).replace(/^(?:\r?\n|)([\s\S]*?)(?:\r?\n|)$/, '$1')

// "C:\Program Files\7-Zip\"
trimmedLine`
C:\Program Files\7-Zip\
`

A space:

const trimmedSpace = (...args) => String.raw(...args).replace(/^([\s\S]*?) ?$/, '$1')

trimmedSpace`C:\Program Files\7-Zip\ `  // "C:\Program Files\7-Zip\" with no space
trimmedSpace`C:\Program Files\7-Zip\  ` // "C:\Program Files\7-Zip\ " with 1 space

And so on.

Even if a string shouldn't be trimmed, a helper can cover this by removing exactly one whitespace character, so extra one could be added to present in resulting string.

Estus Flask
  • 206,104
  • 70
  • 425
  • 565