0

I was trying to get a part of a string by splitting it but the character I'm using to split is a backslash and a hyphen. By researching around SO I found that you have to use double backslashes but that is not working too all I get is the string split in a weird way and a question mark in place of the backslash. How can I solve this? Any help is appreciated. Thanks in advance

x = 'uploads\20211116011817-natural'
console.log(x.split("'\'").pop().split('-')[0]) //expected the number between \ and -
seriously
  • 1,202
  • 5
  • 23
  • You need to escape the backslash in your `x` string literal as well – Phil Nov 17 '21 at 06:14
  • 1
    It's strange to me that you know to escape the backslash in the split call, but not in the original string. You might want to type `'\2021'` into your console and see what that does. – Wyck Nov 17 '21 at 06:14
  • 1
    `'uploads\20211116011817-natural'` does ***NOT*** have a backslash in it. It has an escape sequence but the actual content does not include a backslash (try logging that value), therefore, you cannot split it by a backslash. – VLAZ Nov 17 '21 at 06:15
  • check updated question – seriously Nov 17 '21 at 06:21
  • The core of the problem is the same - the variable `x` does not have a backslash in it. You've now just changed your code so it doesn't even split by a backslash but by two single quotes since *again* the backslash in the `split()` forms an escape sequence. – VLAZ Nov 17 '21 at 06:24
  • that is exactly my question here. I cant manipulate string x because its a path but I need to split it is that possible? @VLAZ – seriously Nov 17 '21 at 06:26
  • that is the reason I used '//' in my split method at first because SO suggested that I will ignore the second one – seriously Nov 17 '21 at 06:27
  • You know how in `"HELLO\nWORLD"` the `\n` is a special _escape sequence_? Well `\202` is special _escape sequence_ too. It is a _legacy octal escape sequence_ resulting in a character with code point 130 (in decimal), which is **202** in base 8 (octal). Try doing `console.log([...x])` to have a look at what each character is. – Wyck Nov 17 '21 at 06:28
  • @seriously if you have a string literal, you should be able to change it. If you have a string with *content* that includes a backslash, that is different to a string literal. String literal `"a\b"` produces the content `ab` but string literal `"a\\b"` produces `a\b`. The only way to get an escape sequence is if it's *in the source code* - a user typing in a text box or getting an existing string from somewhere will not force a single backslash to be interpreted as an escape sequence. If you *really* have a string literal with an escape sequence the best suggestion is to *fix it*. – VLAZ Nov 17 '21 at 06:30
  • @VLAZ wow, so to conclude the only way I could actually split the string and get the char between \ and - is if I got the path and replaced every \ with \\ then run my split then reverted all \\ back to \? – seriously Nov 17 '21 at 06:37
  • @Wyck LOL I didn't get it when you said it in your first comment. Didn't know about \202 being a special char too – seriously Nov 17 '21 at 06:39
  • @seriously. The source code contains a backslash, but the resultant string it produces does not have a backslash in it. Just like the string literal `"HELLO\nWORLD"` does not produce a string with a backslash in it. It has a newline character in it. Do you understand the difference between the source code you write using _JavaScript string literal syntax_ (the characters stored in your source code file) vs the characters stored in the actual string object itself at runtime? – Wyck Nov 17 '21 at 06:54
  • @Wyck oh go it so, in my string x, the / itself is not being produced in the string right? – seriously Nov 17 '21 at 07:00
  • @seriously "Correct!" note: technically you just typed a forward slash instead of a backslash in that comment - but I'll assume you meant \ – Wyck Nov 17 '21 at 13:50

0 Answers0