I got a Rich Text field on my database and i am trying to put it in a text area.
But as i tried to do that i was getting the full area on my output text in .rtf format, like this :
I tried this by assigning with .text value as shown below :
and indeed i got the text on my Text Box area as i wanted it, but… the Fonts and Colors i ve setted on the text box are not “obeyed” by the text which i understant because rtf/htlm has instored formats is self.
So my question is, is there a way to get Rich Text into simple plain text format?
//regular expression to remove the script/html tags
var regex = /(<([^>]+)>)/ig;
//regex to remove the ’ special character with '
var specialRegex = /(’)/ig;
//This will replace all tags with an empty string.
let plainText = richText.replace(regex, "");
//this will replace left over ’ with a '
plainText = plainText.replace(specialRegex, "'");
Now we can also do this in less lines of code by combining lines.
//regular expression to remove the script/html tags
var regex = /(<([^>]+)>)/ig;
//regex to remove the ’ special character with '
var specialRegex = /(’)/ig;
//This will replace all tags with an empty string and replace ’ with a '
let plainText = richText.replace(regex, "").replace(specialRegex, "'");
We can even do it in one line of code! It just can become difficult to read. The answer above is my preferred way to do it.
let plainText = richText.replace(/(<([^>]+)>)/ig, "").replace(/(’)/ig, "'");
Dear Patrick, I’m trying out your code for removing html tags now but may I know how I should define richText? I lack programming knowledge, sorry about that HAHA (the console says my richText isn’t defined)