Textarea line feed

Source: Internet
Author: User

When you use in-row editing in the DataGrid in easyui, the line feed of textarea is saved to the MySQL database \ n.
In textarea, input a carriage return character to read the value \ r \ n from textarea in JS, and then convert it to string at the business layer, which may become a space and be saved to the database, when this value is taken out, it will become a space, so we need to replace \ r \ n that is not displayed.

When JS retrieves textarea:

If you use

VaR STR = Document. getelementbyid ("textarea"). value;
STR = Str. Replace ("\ r \ n", "<br> ");

Then, only the first \ r \ n will be replaced with <br>. How can we replace all n \ r \ n with <br>? Is there an infinite number of replace?

Actually, no. Replace can be combined with a regular expression to replace all \ r \ n at a time.

VaR Reg = new Regexp ("\ r \ n", "G ");
STR = Str. Replace (Reg, "\ r \ n ");

In this way, all \ r \ n in STR is replaced with <br>. Then, it can be saved to the database.

You only need to replace the value after it is retrieved.

VaR Reg = new Regexp ("<br>", "G ");
STT = Str. Replace (Reg, "\ r \ n ");
Document. getelementbyid ("textarea"). value = STR;

For more detailed analysis and application, refer to the use of the replace method and regular expression in JavaScript.

Below are the two functions I have written that can be replaced after being directly put into the universal package.

Function replacetextarea1 (STR ){
VaR Reg = new Regexp ("\ r \ n", "G ");
VaR reg1 = new Regexp ("", "G ");

STR = Str. Replace (Reg, "<br> ");
STR = Str. Replace (reg1, "<p> ");

Return STR;
}

Function replacetextarea2 (STR ){
VaR Reg = new Regexp ("<br>", "G ");
VaR reg1 = new Regexp ("<p>", "G ");

STR = Str. Replace (Reg, "\ r \ n ");
STR = Str. Replace (reg1 ,"");

Return STR;
}

2. in HTML, the content submitted by the <textarea> tag will be converted into "\ r \ n" by carriage return, and the submitted content will be displayed in the <textarea> tag, the carriage return is restored to a normal line feed.

However, when a string with "\ r \ n" is displayed in a common HTML text field, the original line feed effect is converted into a space. Therefore, replace "\ r \ n" in the string with "</BR>" before display ".

In addition, note that the El expression converts "</BR>" line breaks into "& lt;/BR & gt;", "& lt;" and "& gt; "corresponds to" <"and"> ", that is, it is displayed as" </BR> "on the page (HA, El is too intelligent ), to remedy this, JSP labels (<% = %>) are used for display.

Therefore, when you need to display the content submitted by <textarea> in a common text domain, you can directly store unprocessed strings in the database, replace "\ r \ n" with "</BR>" before display, and then use <% = %> to display the content on the page.

3. If spaces and carriage returns are correctly displayed in the textbox submitted to the database
Public String myreplace (string mystr)
{
If (mystr = NULL | mystr = "")
{
Return ("& nbsp ;");
}
Else
{
Mystr = mystr. Replace ("\ n \ r", "<br> ");
Mystr = mystr. Replace ("\ r", "<br> ");
Mystr = mystr. Replace ("\ t ","");
Return (mystr );
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.