How to insert a <CR> (Carriage Return) in a Javascript String?

10,366

In ES6 with string templates you just introduce the carriage return as written text. Also you can use \n to mark carriage return in quoted strings.

const str = `l
e`;

console.log(str);

const str2 = 'l\ne';

console.log(str2);

Share:
10,366
LaPregunta
Author by

LaPregunta

Updated on June 09, 2022

Comments

  • LaPregunta
    LaPregunta almost 2 years

    I need to insert a Carriage Return in a String. As far as I know the \r should do it but here is the problem:

    I wrote this in the browser console: 1\r2 then I get: 12 in return. Now I copy/paste it on Notepad++ https://imgur.com/a/pUW8p

    There is a CR and a LF there. How I can add just a CR?

    Note that you can replace the LF(\n) in notepad, save the file and the LFs are gone.

    • Mardoxx
      Mardoxx over 6 years
      Why do you want this?
    • lumio
      lumio over 6 years
      Probably Notepad++ added an LF by itself.
    • LaPregunta
      LaPregunta over 6 years
      i make a CSV file and send it via Email, the line delimiters must be <CR> only.
    • trincot
      trincot over 6 years
      Do you know you can change the line delimiter to CR only in notepad++? Menu Edit - EOL Conversion - Old Mac Format.
    • LaPregunta
      LaPregunta over 6 years
      Yes, but that does not modify the file, just the way that you look at it in notepad++