5

What's the proper way of handling line feeds on Windows, Mac and Linux. Let's say I have a simple web app that has a textarea and I need to do a string split every new line. Is it safe to convert \r\n and \r to \n before processing the content of the textarea, or do I need to detect what OS is the user is on and apply conditional statement to each?

Sample Code

var content = $('textarea').val();
    content = content.replace(/\r\n|\r|\n/gm, "\n");
    content = content.split("\n");

// Do Something

   content = content.join("\n");

// Update content
$('textarea').val(content);
Vianne
  • 548
  • 1
  • 10
  • 31
  • Possible dupe of https://stackoverflow.com/questions/2159622/jquery-textarea-append-newline-behavior?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – mplungjan Apr 07 '18 at 19:15
  • Just split on `\n` and call it a day. `\r` alone is incredibly rare these days. Splitting on `\n` catches `\r\n` as well. – Brad Apr 07 '18 at 20:11
  • All current JavaScript engines use `\n` for a newline, for read and write purposes. Even when you work with, say text copy&pasted into a textarea from a different source, the browser is in charge of handling this, and present your script with `\n` newlines and nothing else. – CBroe Apr 07 '18 at 20:11
  • Possible duplicate of [jQuery textarea append newline behavior](https://stackoverflow.com/questions/2159622/jquery-textarea-append-newline-behavior) – Heretic Monkey Apr 07 '18 at 21:19

1 Answers1

1

no just write your code with '\n' and that's enough. since get the text from textarea means exporting to a text form that can be represent as file or whatever and then so from https://en.wikipedia.org/wiki/Newline

When writing to a file, device node, or socket/fifo in text mode, '\n' is transparently translated to the native newline sequence used by the system, which may be longer than one character. When reading in text mode, the native newline sequence is translated back to '\n'. In binary mode, no translation is performed, and the internal representation produced by '\n' is output directly.

it's very depends on what you want to do with it. if you send it back to your server then it's depends on you if you want to export it to user as a file then it's OS. but generally it's almost always safe to just use '\n' since it's been more common and generally accepted.

$(function(){
var $text = $("#text");
  $text.val( $text.val() + "Hello \nThis is '\\r' a \r" + // here
                  "multi-line (\\r\\n) \r\n text with" + // here CRLF
                  " differents \n linefeeds" );
  var s = $text.val().split('\n');
  // notice line 3 on output, it's been feed by '\r'
  // and we split only by '\n' 
  $text.val( s.map( (ss,n) => n + "\t" + ss ).join("\n") );
  $("#wonder").on("click",function() {
    $text.val( $text.val().split("\n").join("\t\\r\r") );
    // var s = $text.val();
    // either sending it via AJAX or
    // export it to user, it's safe to go with '\n' .
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="text" rows="7" cols="37"> Hey<br>there\n

</textarea> <br>
<button id="wonder">wonder</button>
nullqube
  • 2,959
  • 19
  • 18
  • 1
    That quote is from the section about the C language. The question isn't using C, but JavaScript and isn't writing to a "file, device node, or socket/fifo" but a node in the DOM/HTML. – johannes Apr 07 '18 at 20:00
  • This seems to be relevant for interacting with the OS, but not for JS in the browser (implied by the questin being tagged [jquery]) – Bergi Apr 07 '18 at 20:05
  • @nullqube the time it will be handled is by the time it's rendered to the screen (depending on operating system - on a UNIX system the X server isn't part of the core operating system) and the question is about handling it in memory, I believe the browser or.JavaScript runtime will normalize it, but that's not related to that quote. – johannes Apr 07 '18 at 20:15
  • Freetype or whatever still aren't the OS. And the user takes and puts it in a Textarea. In a Textarea an
    is not an line break, but "
    " put into a Textarea via jQuery will be rendered as those four characters. This all doesn't answer whether it is guaranteed that line breaks will be represented as \n or \r\n either by any applicable standard or de facto all notable implementations.
    – johannes Apr 07 '18 at 20:30