0

I need to replace a \n\r from a string multiple times. I managed to replace the first occurrence, but it doesn't work for more than one time. Something is not right with the escaping. I've tried:

$ node
> 'string\n\rstring\n\r'.replace('\n\r', '');
'stringstring\n\r'
> 'string\n\rstring\n\r'.replace('/\n\r/g', '');
'string\n\rstring\n\r'
> 'string\n\rstring\n\r'.replace('/\\n\\r/g', '');
'string\n\rstring\n\r'
> 'string\n\rstring\n\r'.replace('/\\\n\\\r/g', '');
'string\n\rstring\n\r'
> 'string\n\rstring\n\r'.replace('/\\\\n\\\\r/g', '');
'string\n\rstring\n\r'

How do I get stringstring?

mles
  • 4,534
  • 10
  • 54
  • 94
  • 1
    Use your second solution, just as a regex literal: `.replace(/\n\r/g, '')`, although the usual Windows style line break char sequence is `\r\n`. – Wiktor Stribiżew Oct 24 '19 at 13:21
  • Right it was `\r\n`. Ok so the error was to put the regex into ticks (`' '`). If you put your comment in an answer I'll mark it as correct. – mles Oct 24 '19 at 13:28
  • 1
    There is no need for an answer, it is a very common typo. – Wiktor Stribiżew Oct 24 '19 at 13:29
  • The `\r\n` sequence can easily be remembered by the meaning behind them. First place the cursor back at the start at the of the line (carriage return) then move a line down (line feed). – 3limin4t0r Oct 24 '19 at 13:32

0 Answers0