How can I replace HTML
with new line character \"\\n\"
or
This function considers whether to remove or replace the tags such as
,
,
, .
/**
* 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
,
,
,
return str.replace(/<\s*\/?br\s*[\/]?>/gi, replaceStr);
}
In your case, you need to use replaceMode
. For eaxmple: br2nl('1st
2st', true)
Demo - JSFiddle
JavaScript nl2br & br2nl functions