0

Assume the following code:

var text = "John\nDoe";
console.log(text);

I end up with two lines...

John
Doe

The variable text is passed into my function as a call to a web service, but the special chars are interpreted as a carriage return which breaks my hashing algorithms. They are not interpreted as standard chars and giving bad hash values.. How do I get node.js to interpret them as literals?

I have no control over the values passed in. Simply typing a double backslash is not an option.

I do not wish to remove all line breaks. I want the line breaks to be string literals. I want the \n as as string literal, and not be removed.

barrypicker
  • 9,740
  • 11
  • 65
  • 79
  • Try to escape it? – Chang Alex Jul 12 '22 at 01:23
  • @ChangAlex - The values are passed into my function. I have tried to escape it using `/\\\n/g, '\\\n'` but I end up at the same place. – barrypicker Jul 12 '22 at 01:25
  • What is "your function" like? – Chang Alex Jul 12 '22 at 01:28
  • And you know `using /\\\n/g, '\\\n'` does nothing? – Chang Alex Jul 12 '22 at 01:29
  • Maybe `\n` to `\\n`? – Chang Alex Jul 12 '22 at 01:30
  • here is a JSFiddle I was trying.. - https://jsfiddle.net/wgns4b79/ – barrypicker Jul 12 '22 at 01:30
  • Maybe [this](https://jsfiddle.net/9fsokh24/1/) – Chang Alex Jul 12 '22 at 01:31
  • @ChangAlex - Yes, I think that is exactly what I am trying to get... Thank you! I thought I needed to escape both the backslash and the `n`. – barrypicker Jul 12 '22 at 01:32
  • If this still not working in "your function", you may show us the code of that. Because we don't know what "your function" actually do with string. – Chang Alex Jul 12 '22 at 01:33
  • This seems to be working. Please add an answer and I will award points. Thank you! – barrypicker Jul 12 '22 at 01:35
  • 1
    It'd been changed to duplicate, but that's ok. I'm glad to help you. – Chang Alex Jul 12 '22 at 01:38
  • "*the special chars are interpreted as a carriage return*" - the characters **are** carriage returns. I suggest you fix your hashing algorithm to deal with them and not break. – Bergi Jul 12 '22 at 02:01
  • @Bergi, if considering ASCII, carriage return is character 13, the backslash (a legitimate character) is character 92, and the lowercase letter 'n' is character 110. The concept of `\n` representing ASCII character 13 is a language dependent concept. This data was sent via a web service call and character 13 was not sent, but instead a byte array including characters 92 and 110 were sent. These characters are *not* carriage returns, but were interpreted that way because Javascript assumes they are. – barrypicker Jul 12 '22 at 15:21
  • @barrypicker JavaScript parses these two characters into a newline when evaluating string literals, when executing code. When a web server receives the two characters in a http body, it does not automatically interpret them differently, they're still raw data - a byte array as you say. This has nothing to do with JavaScript - I hope you're not interpreting the byte array as code?! No, something in your server code says to parse the byte array into a string, and that code needs fixing. Please post it. Also, the hashing code still needs fixing, as someone could also send a raw character 13. – Bergi Jul 12 '22 at 15:40
  • @Bergi - thank you for your comments. I do not have access to the source code submitting the web request. The hashing is working as expected - if someone passed char(13) it should be invalid, and it is. The characters char(92), and char(110) are passed in and need to be validated as a literal, not as char(13). By escaping it (per Chang Alex) the program seems to be hashing correctly. Thank you for your assistance.. – barrypicker Jul 12 '22 at 18:37
  • @barrypicker "*The characters char(92), and char(110) are passed in and need to be validated as a literal*" - yes. If you need help with this, [edit] your question to show us the code that is supposed to do this. You shouldn't need to un-escape the string afterwards, you shouldn't parse it like a JS(ON?) string in the first place. – Bergi Jul 12 '22 at 19:28

0 Answers0