Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert html string into plain text in React? [duplicate]

How do I convert html string (with a lot of tags etc.) into plain text in React? Any npm module maybe?

I haven't found any methods for converting html string to plain text. I need this because I want to save a text file edited in Ckeditor 5 as a plain text file, but Ckeditor 5 manages data in html or markdown format.

like image 931
Petro Ivanenko Avatar asked Jun 04 '26 00:06

Petro Ivanenko


1 Answers

You can write your own piece of code to make that happen, no library needed.

var htmlString = "<h1><b>test</b></h1>";
var plainString = htmlString.replace(/<[^>]+>/g, '');

console.log(plainString ); // you will have your plain text

or

function getText(html){
    var divContainer= document.createElement("div");
    divContainer.innerHTML = html;
    return divContainer.textContent || divContainer.innerText || "";
}

var yourString= "<div><h1>Hello World</h1>\n<p>We are in SOF</p></div>";

console.log(getText(yourString));
like image 74
Mr.Throg Avatar answered Jun 06 '26 12:06

Mr.Throg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!