How to remove HTML tags from content?


#1

Hello there,

I want to add COPY TEXT(Message) functionality in my app. so i have used below plugin to copy text.
http://ngcordova.com/docs/plugins/clipboard/

Copy function works fine, but TEXT(Message) has some HTML tags its also copied with text. like

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

But i want to copy like this,

Night is a Theater,
Dream is a Movie,
God is the Director,
Nature is the Producer,
Your the Hero ‘Enjoy’ the night with Sweet Dreams…

So, Can you please help me on this? how can i fix it?

Thank you


#2

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


Add HTML tags inside string
#3

Its working, thanks a lot.