![](/img/trans.png)
[英]How to replace the \n characters in the body section of an html with <br>
[英]How to replace an HTML <br> with newline character “\n”?
如何用换行符“\\n”替换 HTML <BR> <BR/> or <BR />
您正在寻找 PHP 的nl2br()
的nl2br()
。 这应该可以完成这项工作:
function br2nl(str) {
return str.replace(/<br\s*\/?>/mg,"\n");
}
一个便宜的功能:
function brToNewLine(str) {
return str.replace(/<br ?\/?>/g, "\n");
}
es.
vat str = "Hello<br \>world!";
var result = brToNewLine(str);
结果是:“你好/nworld!”
此函数考虑是否删除或替换<br>
、 <BR>
、 <br />
、 </br>
。
/**
* This function inverses text from PHP's nl2br() with default parameters.
*
* @param {string} str Input text
* @param {boolean} replaceMode Use replace instead of insert
* @return {string} Filtered text
*/
function br2nl (str, replaceMode) {
var replaceStr = (replaceMode) ? "\n" : '';
// Includes <br>, <BR>, <br />, </br>
return str.replace(/<\s*\/?br\s*[\/]?>/gi, replaceStr);
}
在您的情况下,您需要使用replaceMode
。 例如: br2nl('1st<br>2st', true)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.