Let's say I have a string like this '"\"'. The length of this string is 2. So I wonder if there's any way I can differentiate the first " and the second \"? Thanks.
I'm trying to parse a string and I want to turn on/off a flag when " is encountered, but ignore the \".
It's not possible. The two characters are equal to each other, because the backslash gets interpreted as an unnecessary escape character, and gets discarded:
const str = '"\"';
console.log(str[0] === str[1], str.length);
If you wanted to put a literal backslash into the string, either put two backslashes in there:
const str = '"\\"';
console.log(str.length);
Or use String.raw, in which (nearly) every character gets interpreted literally, without escaping:
const str = String.raw`"\"`;
console.log(str.length);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With