+ Start a Discussion

String HTML char from text field??

So here is my issue. I have some text fields that can store HTML formatted data in SFDC. For example "<strong>Test</strong>". What I need to do is get a character count of the non HTML characters in that field. So for our example I want to know that there is 5 char in it not 22 char.


I can do this in Javascript by using a regular expression:


var re= /<\S[^>]*>/g; var test = "<strong>Test</strong> test = test .replace(re,"");


But I woudl like to do it in APEX and store the char count in a field on that record. I am not Java guy so all the examples seem to drive me to have a beer. Can anyone point me in the right direction?  All I really need to know how to do is perform the above Javascript code in APEX...and I think I need to user the "replaceAll" method.



Thanks in advance!





Public Static String getEmailFromSubjecct(String Subject){

// First, instantiate a new Pattern object "MyPattern"
String emailRegex = '(\\w+)@(\\w+\\.)(\\w+)(\\.\\w+)*';
Pattern MyPattern = Pattern.compile(emailRegex);

// Then instantiate a new Matcher object "MyMatcher"
Matcher MyMatcher = MyPattern.matcher(Subject);

boolean hasMatch = MyMatcher.matches();
String match = MyMatcher.group(0);
return match;




I exactly can't guess what your purpose is. However I believe that following code will give you an idea.

If this is not what you are looking for, please supply more info and then I can help you.


public with sharing class LengthCounter{

public Integer getLength() {

Integer charCount = 0;

Pattern pat = Pattern.compile('<[^>]*>([^><]*)<.*');

Matcher matcherInstance = pat.Matcher('<strong>Test</strong>');

if (matcherInstance.find()) {

charCount = matcherInstance.group(1).length();


return charCount;