开发者

Replace \n with <br> and \r\n with <p> in javascript

I need JS that will remove 开发者_StackOverflow社区any HTML tags, and then replace newlines with </p><p> and line breaks with <br/>. The string value is coming from a textarea and I understand Linux, Mac and Windows all format newlines differently so I need to take that into account. Thanks!


\n and \r\n are equivalent. Linux uses the former, Windows uses the latter.

What you want to do is replace all cases of \n\n and \r\n\r\n with <p></p> and case of simply \n or \r\n with <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 />");

This assumes there is no html in your text.


I think

value.replace(/\\n\\n/g, "</p><p>");
value.replace(/\\n/g, "<br/>");

will do the trick.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜