Home > Software design >  How to convert html content in to one string
How to convert html content in to one string

Time:04-04

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

CodePudding user response:

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

  • Related