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!
我需要JS来删除任何HTML标记,然后用
和
的换行符替换换行。字符串值来自于一个textarea,我理解Linux, Mac和Windows都有不同的格式,所以我需要考虑到这一点。谢谢!
2 个解决方案
#1
18
\n and \r\n are equivalent. Linux uses the former, Windows uses the latter.
\n和\r\n是相等的。Linux使用前者,Windows使用后者。
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 />
你所要做的是,将所有的\n\n \ \ \n\ \n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ * \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ * \ \ \ \ \ \ \ * \ \ \ \ \ * \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
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.
这里假设文本中没有html。
#2
1
I think
我认为
value.replace(/\\n\\n/g, "</p><p>");
value.replace(/\\n/g, "<br/>");
will do the trick.
上大做文章。
#1
18
\n and \r\n are equivalent. Linux uses the former, Windows uses the latter.
\n和\r\n是相等的。Linux使用前者,Windows使用后者。
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 />
你所要做的是,将所有的\n\n \ \ \n\ \n\ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ * \ \ \ \ \ \ \ \ \ \ \ \ \n\ \ \ \ \ \ \ * \ \ \ \ \ \ \ * \ \ \ \ \ * \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \
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.
这里假设文本中没有html。
#2
1
I think
我认为
value.replace(/\\n\\n/g, "</p><p>");
value.replace(/\\n/g, "<br/>");
will do the trick.
上大做文章。