I want to strip tags from a html, but preserves it's line breaks.
I want the behaviour like copying the text in browser and pasting it in notepad.
For example, a code that converts:
<div>x1</div><div>x2</div>
to x1\nx2
<p>x1</p><p>x2</p>
to x1\nx2
<b>x1</b><i>x2</i>
to x1x2
x1<br>x2
to x1\nx2
Removing all tags not works (/<.*?>/g).
Also creating a dummy <div> and settings it's innertHTML
and read it's textContent
will remove line breaks.
Any Help?
Try:
function strip_tags(str){
return str
.replace(/(<(br[^>]*)>)/ig, '\n')
.replace(/(<([^>]+)>)/ig,'');
}
var str = '<div>x1</div><div>x2</div><br>'+'<p>x1</p><p>x2</p>'+'<b>x1</b><i>x2</i>';
This will strip the tags and replace <br />
or <br>
with new lines, but adding new lines for block elements requires quite some time to come up with a solution.