0

It's a React-Native application, so I can not go for DOM

I have a HTML as a string let's say,

let str = "<p>Research not to discover what&#8217;s already there, but to discover what&#8217;s not.</p>\n",

I am able to parse it using Regex

str.replace(/<\/?[^>]+(>|$)/g, "")

But the output of this is,

"Research not to discover what&#8217;s already there, but to discover what&#8217;s not.
"

which is still have HTML code in it what&#8217;s How I can I replace it with what's

Praveen Rana
  • 440
  • 7
  • 21

1 Answers1

1

You could use something like This answer suggests, but this answer only works for numeric entities, not for things sur as &amp;

I guess it depends on how you know what kind of html you might be getting. Otherwise you will have to use a library to decode those HTML entities.

Tony
  • 91
  • 4
  • Thanks for the response, but It's not working for numeric entities also, any other approach you want to suggest? – Praveen Rana Jul 25 '18 at 18:11
  • 1
    The linked answer almost works, you just need to change the regex from `/([0-9]{1,3});/gi` to `/([0-9]{1,4});/gi` because hmtl entities can have 4 characters. That being said, since this is a public API and not your own data, I would recommend you to rather use a library for the reasons cleary explained [here](https://stackoverflow.com/a/23831239/4711321) – Tony Jul 25 '18 at 19:58
  • Yes, I found the solution using the name link, Thankyou @Tony – Praveen Rana Jul 26 '18 at 07:06