Which character represents a new line in the text area

Just quick, but I want me to catch cross-platform options.

I like to convert newlines entered in the text area to [comma] so that the output can be presented on one line, my question is ...

Currently, sending from google chrome when I look at the value, I find that it uses \r\n for newlines. If I replaced \r\n , I know that it will work for chrome on windows 7, but what about other platforms, are there any options that other browsers will insert in a new line inside the text area?

+72
html newline textarea
Jan 08 '13 at 14:27
source share
4 answers

According to HTML specifications, browsers should canonize line breaks when entering a user in CR LF ( \r\n ), and I donโ€™t think any browser is wrong. Reference: Article 17.13.4 Types of content content in the HTML 4.01 specification.

In HTML5 drafts, the situation is more complicated, since they also process processes inside the browser, and not just the data that is sent to the form processor on the server side when the form is submitted. According to them (and browser practice), the value of the textarea element exists in three ways:

  • the raw value entered by the user is not normalized; it may contain a pair of CR, LF or CR LF;
  • an internal value called an "API value" where line breaks are normalized to LF (only);
  • view value, where line breaks are normalized to CR LF pairs, in accordance with Internet conventions.
+91
Jan 08 '13 at 14:37
source share

Speaking specifically about text areas in web forms, for all text areas, on all platforms, \r\n will work.

If you use anything else, you will cause problems with cut and paste on Windows platforms.

Line breaks will be canonized by window browsers when submitting the form, but if you submit the form to the browser using \n linebreaks, you will find that the text will not be copied and pasted correctly, for example, in notepad and text field.

Interestingly, although the Unix end-of-line agreement has \n , the standard in most text-based network protocols, including HTTP, SMTP, POP3, IMAP, etc., is still \r\n . Yes, this may not make much sense, but this story and evolving standards are for you!

+11
Jan 08 '13 at 14:29
source share
 
- Line Feed and 
 Carriage Return 

As HTML objects, it avoids showing / r / n as characters

+7
Mar 22 '14 at 19:39
source share

It looks like according to the HTML5 specification, the value property of the textarea element should return '\ r \ n' for a new line

The value element is defined as the initial value of the element using the following transformation:

Replace each occurrence of the โ€œCRโ€ symbol (U + 000D) followed by the โ€œLFโ€ symbol (U + 000A), and each occurrence of the โ€œLFโ€ (U + 000A) symbol preceded by the symbol โ€œCRโ€ (U + 000D) ), through a two-character string consisting of U + 000D CARRIAGE RETURN "CRLF" (U + 000A).

After referencing the โ€œvalueโ€, it is clear that it refers to the value property available in javascript:

Form controls matter and validation. (The latter is used only for input elements.) They are used to describe how the user interacts with the control.

However, in all five major browsers (using Windows, 11/27/2015), if '\ r \ n' is written to the text field, '\ r' is lost. (For verification: var e = document.createElement ('textarea'); e.value = '\ r \ n'; alert (e.value == '\ n');) This applies to IE as of version v9. Prior to this, IE returned '\ r \ n' and converted both '\ r' and '\ n' to '\ r \ n' (which is an HTML5 specification). So ... I'm confused.

To be safe, it is usually sufficient to use "\ r? \ N" in regular expressions, and not just "\ n", but if a newline is to be known, an application like this can be tested ..

+5
Nov 27 '15 at 16:28
source share



All Articles