1

I have a line of data like this:

1•#00DDDD•deeppink•1•100•true•25•100•Random\nTopics•1,2,3,0•false in a text file.

Specifically, for my "problem", I am using Random\nTopics as a piece of text data, and I then search for '\n', and split the message up into two lines based on the placement of '\n'.

It is stored in blockObj.msg, and I search for it using blockObj.msg.split('\n'), but I kept getting an array of 1 (no splits). I thought I was doing something fundamentally wrong and spent over an hour troubleshooting, until on a whim, I tried

blockObj.msg = blockObj.msg.replace(/\\n/g, "\n")

and that seemed to solve the problem. Any ideas as to why this is needed? My solution works, but I am clueless as to why, and would like to understand better so I don't need to spend so long searching for an answer as bizarre as this.

I have a similar error when reading "text" from an input text field. If I type a '\n' in the box, the split will not find it, but using a replace works (the replace seems pointless, but apparently isn't...)

obj.msg = document.getElementById('textTextField').value.replace(/\\n/g, "\n")

Sorry if this is jumbled, long time user of reading for solutions, first time posting a question. Thank you for your time and patience!

P.S. If possible... is there a way to do the opposite? Replace a real "\n" with a fake "\n"? (I would like to have my dynamically generated data file to have a "\n" instead of a new line)

  • 1
    You have `\n` *as text*. However the escape sequence `\n` when used *in a string literal* signifies a newline. Which the literal text of "backslash followed by the letter **n**" is not. You don't need to replace it, you just have to split by the sequence "backslash followed by the letter **n**" which in a string literal is expressed as `\\n` - the backslash is escaped to make it a literal backslash. – VLAZ Aug 03 '22 at 10:03
  • Basically you can say, this boils down to the difference between _data_, and _code_. – CBroe Aug 03 '22 at 10:05
  • [JavaScript backslash (\) in variables is causing an error](https://stackoverflow.com/q/3903488) | [How can I use backslashes (\) in a string?](https://stackoverflow.com/q/10041998) | [Escaping backslash in string - javascript](https://stackoverflow.com/q/8618374) | [Escape new lines with JS](https://stackoverflow.com/q/25921319) | [How to escape backslashes from a string in javascript](https://stackoverflow.com/q/63056335) | [Is it possible to separate "\t" to char '\' + 't'](https://stackoverflow.com/q/7649372) | – VLAZ Aug 03 '22 at 10:11

2 Answers2

3

It is stored in blockObj.msg, and I search for it using blockObj.msg.split('\n'),

In a JavaScript string literal, \n is an escape sequence representing a new line, so you are splitting the data on new lines.

The data you have doesn't have new lines in it though. It has slash characters followed by n characters. They are data, not escape sequences.

Your call to replace (blockObj.msg = blockObj.msg.replace(/\\n/g, "\n")) works around this by replacing the slashes and ns with new lines.

That's an overcomplicated approach though. You can match the characters you have directly. blockObj.msg.split('\\n')

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I... feel like vomiting a little, but this seems to make sense. Honestly, I probably should have mentioned that I have very little idea of what I'm doing with .replace, and I have been just using it with copy/paste as needed. I always wondered why it had so many slashes... but that explains one of the slashes (and it solves my problem). Thanks! – Nicholas Guerrero Aug 03 '22 at 10:14
1

in your text file

1•#00DDDD•deeppink•1•100•true•25•100•Random\nTopics•1,2,3,0•false

means that there are characters which are \ and n thats how they are stored, but to insert a new line character by replacement, you are then searching for the \ and the n character pair.

obj.msg = document.getElementById('textTextField').value.replace(/\\n/g, "\n")

when you do the replace(/\\n/g, "\n")

you are searching for \\n this is the escaped version of the string, meaing that the replace must find all strings that are \n but to search for that you need to escape it first into \\n

EDIT

/\\n/g is the regex string..... \n is the value... so /\REGEXSTUFFHERE/g the last / is followed by regex flags, so g in /g would be global search

regex resources

test regex online

Dean Van Greunen
  • 5,060
  • 2
  • 14
  • 28
  • 1
    I suck at using replace, and have been copying and pasting as needed. I always thought the `replace(/\` part was standard, and that i was just using `\n` afterwards. Your explanation really helped. Thanks. – Nicholas Guerrero Aug 03 '22 at 10:17