function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

How to get a rich text area content without HTML markups

Hello everybody, This is my first Salesforce post.


In my controller, I use a query to get the field related to a rich text area.

My need is to get, for example, 200 first character of a rich text area without HTML markup.


I don't know if a method already exists to perform this because when I try to change the field type from rich text area to text area, Salesforce propose if I want to keep or not the HTML markup of rich text area contents.


Thank you


Ralph CallawayRalph Callaway

You can use a regex to replace any html tags.


String input = yourObject.rich_text_field__c;
input = input.replaceAll('<[/a-zAZ0-9]*>','');

Why do you want to strip out the html content?


I guess


fieldValue = fieldValue.replaceAll('<[^>]+>',' ');


is a better regex. At least that's what served me well for the last 15 years.


Is it possible to replace all HTML tags except hyperlinks (<a> tags) and image tags (<img .. >)?





Currently I am facing same issue. I want to remove other HTML tags except hyperlinks and Image tags. Have you solved the issue.


Not yet.

Elke MeitznerElke Meitzner
Hi Hari,
have you ever found a solution for this? I'm just struggling with exactly this problem...
I want to copy content of a rich text field into a field which is only long text. As soon as there is any formatting in the rich text field the long text field contains any Markup/HTML  tags and gets worse. Since the target field is from a managed package (Agile Accelerator) I'm not free to change the field type there.
I'm completely open how to fill the new field: ProcessBuilder, Workflow rule/Field update or apex trigger - but anyway, the problem remains.
I tried to remove all HTML tags from the text in the trigger, but I also loose newlines or tabs and it still looks ugly.

Thanks and best regards - Elke
Amita Patil 10Amita Patil 10
I used below regex this resolved my issue :
String s= fieldName.replaceAll('<[^>]+>',' ');
Megan D MoodyMegan D Moody
I'm trying do this in a formula field in flow. There's no replaceall function available. Does anyone know how to do this with flow? 
Zachary Alexander 22Zachary Alexander 22
In case anybody is still trying to figure out how to eliminate specific tags, here is a good resource:

According to the article, an example for eliminating a specific kind of tag would be the following:

String target = someString.replaceAll("(?i)<td[^>]*>", "");
Srinivas Cherupalli 12Srinivas Cherupalli 12
Use the below code

String temp = temp.replaceAll('</?h.*?>|</?s.*?>|</?f.*?>','');
String str1 = '<b>Hello World</b>';
String str2 = str1.stripHtmlTags();