Why can't String.raw end with a backslash? [duplicate]
Asked Answered
L

2

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 :(

Locomotive answered 24/4, 2020 at 20:22 Comment(1)
Today I had a similar situation in python. Python's implementation of raw strings is better (for this use-case) than EcmaScript's in my opinion.Locomotive
P
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.

Pleasant answered 24/4, 2020 at 20:24 Comment(6)
I guess this is low-level parser baggage that String.raw couldn't overcome. Thanks for the explanation.Locomotive
I think it's intentional. How else could you put a backtick in the string?Pleasant
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.Locomotive
This doesn't seem like a correct solution. String.raw`...\\` gives me "...\\\\", not "...\\".Haberman
@Haberman 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.Pleasant
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`...${"\\"}`Haberman
V
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.

Valvular answered 23/7, 2020 at 9:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.