+ 9

JS. How to detect backslash in multiline text

I want to print multiline text in the same format, in 2 lines but can't detect backslash used in the orginal text!! var text = "some text in line 1\ more text in line 2" for (let i=0; i<text.length; i++) if (text[i] == "\\") document.write("<br>") else document.write(text[i]) document.write(text) misses newlines & spaces!!

22nd Sep 2021, 4:21 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
2 Answers
+ 7
Abhay Thx, but I see that backslash is totally ignored, disappeared. Try: text.length https://code.sololearn.com/Wc2sBvdzHni7/?ref=app
22nd Sep 2021, 8:07 PM
Janusz Bujak 🇵🇱 🇺🇦
Janusz Bujak 🇵🇱 🇺🇦 - avatar
+ 2
It replaces "\" with a space. You can use text[i].charCodeAt(0) to check the ascii value of each character . That's how i came to know thar "\" is replaced by a space! Edit: Janusz Bujak 🇵🇱 sorry and ty for letting me know , just realised that it isn't replacing backslash with space but it is ignored completely !
22nd Sep 2021, 5:21 PM
Abhay
Abhay - avatar