javascriptsanitizationstrip-tags

Strip tags with javascript and handle line breaks


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:

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?


Solution

  • 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.

    Here is a demo