在javascript中将\n替换为<br>和\ r \n用<p>替换

ric*_*ich 7 javascript string

我需要JS将删除任何HTML标记,然后用换行符换</p><p>行换行符<br/>.字符串值来自textarea,我理解Linux,Mac和Windows所有格式换行都不同,所以我需要考虑到这一点.谢谢!

Joe*_*oel 21

\n和\ r \n是等效的.Linux使用前者,Windows使用后者.

您想要做的是用\n <p></p>简称\n \n\\\r \n\\\r<br />

result = "<p>" + text + "</p>";
result = result.replace(/\r\n\r\n/g, "</p><p>").replace(/\n\n/g, "</p><p>");
result = result.replace(/\r\n/g, "<br />").replace(/\n/g, "<br />");
Run Code Online (Sandbox Code Playgroud)

这假设您的文本中没有html.