Skip to content
Advertisement

How to convert html content in to one string

let html=”

Some data some more data with some other data need to convert into string

I want it should be become a single string like str=”Some data, some more data, some other data, need to convert into string”;

This I need in Reactjs functionality.

In comment section I have shared the details more

Advertisement

Answer

You can use a regex to replace the HTML tags, something like:

const getNormalisedString = (str) => (str ?? '').replace(/</?[^>]+(>|$)/g, "");

const input = "<p>Some data <span> some more data </span>  with  <a> some other data</a>  need to convert into string </p>";

console.log(getNormalisedString(input));

But I would recommend using a parser, and if that is not available then some library to sanitize your HTML string like sanitize-html

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement