How to remove HTML tags from content?

Try this:

strip(html: string) {
	return html.replace(/<(?:.|\n)*?>/gm, '');
}

br2nl(html: string) {
	return html.replace(/<br( \/|\/|)>/gm, '\r\n');
}

Use it like so

strip(br2nl("<p>Night is a Theater,<br>Dream is a Movie,<br>God is the Director,<br>Nature is the Producer,<br>Your the Hero Enjoy the night with Sweet Dreams…</p>"));

br2nl would match the following

<br/>
<br />
<br>

Bear in mind that strip might also replace what looks like tags that the user has written, if not encoded. E.g. say I’m writing a post with the following text:

Here's a test text, if I did <this> then the outcome would be wrong

And the HTML equivalent would be

<p>Here's a test text, if I did <this> then the outcome would be wrong</p>

Then the strip would do this

Here's a test text, if I did  then the outcome would be wrong

Quick update: Basically all you’d need to encode would be < and >, so replace them with &lt; and &gt; respectively. So the HTML would like like this

<p>Here's a test text, if I did &lt;this&gt; then the outcome would be right*</p>

* Right as in you might need to decode them again before displaying them to the user

2 Likes