I have a string like this
str = 'zone\city'
I want to split this string by the backslash. Tried with str.split('\\')
but it does not work.
I know backslash is not escaped in str
String. Its the reason why i can not split. However, then I tried replace the backslash with another character but could not find any workable solution. Anyone can advise me a solution in javascript to split the above string. Thanks in advance.
Asked
Active
Viewed 169 times
-1

tomerpacific
- 4,704
- 13
- 34
- 52

Kevin
- 407
- 2
- 7
- 22
-
2`str = 'zone\city'` literal doesn't have a backslash character in it. You need to use something like `String.raw\`zone\city\`` or `"zone\\city"` – adiga Sep 07 '20 at 07:58
-
`str = 'zone\city'` is equal to `str = 'zonecity'`, try `str = 'zone\\city'` – Shiz Sep 07 '20 at 07:59
1 Answers
1
A single backslash will be escaped from the string:
str = 'zone\city'
console.log(str)
You need to escape the one in the string too:
str = 'zone\\city'
console.log(str.split('\\'))
console.log(str)

Greedo
- 3,438
- 1
- 13
- 28
-
Yes, I know this. I'm wondering how to escape this single backslash. Because I get the value from another service. – Kevin Sep 07 '20 at 08:01
-
@Liam, I have already mentioned it in the description. Anyways, thank you for ur time. – Kevin Sep 07 '20 at 08:04