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 ;}