admin管理员组文章数量:1193389
I need JS that will remove 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!
I need JS that will remove 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!
2 Answers
Reset to default 24\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.
本文标签: stringReplace n with ltbrgt and rn with ltpgt in javascriptStack Overflow
版权声明:本文标题:string - Replace n with <br> and rn with <p> in javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738427733a2086221.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
\n
and\r\n
can both be called newlines or line breaks. I think a use case might be beneficial here. – cmptrgeekken Commented Mar 5, 2010 at 21:31