Javascript obtains the carriage return line break in the textarea text box.

Source: Internet
Author: User

Author: Wang Xianrong

Conclusion

Javascript obtains the string in the textarea text box. If a carriage return or line break is contained,\ NInstead of what we usually see\ R \ n".

Problems Found

When creating a small website today, You need to replace the English, Chinese, and English semicolons entered by the user with the Chinese semicolons. During the compilation of JavaScript Functions, you cannot always replace the carriage return correctly.

For example, in textarea, enter:

Test string 1

Test string 2

We save the input string in textarea to the variable content. The following replacement statement will not replace the carriage return with the Chinese semicolon.

Content = content. Replace ("\ r \ n ",";");

Solve the problem

Using the Google algorithm (search for the keyword "javascript substr \ r \ n" in Google), findArticleAsk Ben: javascript replace and multiple lines/line breaks, which provides detailed explanations.

Expansion

If we want to obtain the carriage return Position entered by the user in textareaCode:

VaR Pos = content. indexof ("\ n ");

If we need to replace the carriage return with another character, such as a semicolon, we need to use code similar to the following:

Content = content. Replace ("\ n ",";");

Complete code

The complete code in this article is as follows:

 
// Replace the English semicolon with a Chinese semicolon, a Chinese or English comma, or press ENTER function replaceseperator (mobiles) {var I; var result = ""; var C; for (I = 0; I <mobiles. length; I ++) {c = mobiles. substr (I, 1); If (C = ";" | C = "," | C = ", "| C =" \ n ") Result = Result +"; "; else if (C! = "\ R") Result = Result + C;} return result ;}
Related Article

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.