Does anybody know why the normalizeSpace() or replaceAll() doesn't work with my rich text area field? I tried entering same value that I have on the rich text area and manually adding it on a variable(String manual). They don't show the same result.

        for(ENQ_Surveillance_Reports__c eachSR : listSurveillanceReports){
        String textArea = eachSR.Test_Rich_Text_Area_Field__c;
        String manual = 'Test              replace spaces';
        System.debug('##textArea: ' + textArea);

        System.debug('@@@textAreaNormalized: ' + textArea.normalizeSpace());
        System.debug('###manualNormalized: ' + manual.normalizeSpace());

        replaced = textArea.replaceAll('\\s\\s+','REPLACED');
        String manual1 = manual.replaceAll('\\s\\s+','REPLACED');
        System.debug('@@@textAreaReplaced: ' + replaced);
        System.debug('###manual1: ' + manual1);

This is what shows on the debug logs for normalizeSpace().

17:04:49.0 (50913259)|USER_DEBUG|[176]|DEBUG|##textArea: Test replace                 multiple spaces with a single space.
17:04:49.0 (50961950)|USER_DEBUG|[178]|DEBUG|@@@textAreaNormalized: Test replace                 multiple spaces with a single space.
17:04:49.0 (50993445)|USER_DEBUG|[179]|DEBUG|###manualNormalized: Test replace spaces

This is what shows for replaceAll().

17:04:49.0 (51094788)|USER_DEBUG|[183]|DEBUG|@@@textAreaReplaced: Test replace                 multiple spaces with a single space.
17:04:49.0 (51115244)|USER_DEBUG|[184]|DEBUG|###manual1: TestREPLACEDreplace spaces
  • 1
    The normalizeSpace() works as expected. You are most likely dealing with a white space character that isn't the normal space bar character. I know of a very common one but can't come up with it right now. I'll try to google it for you. You can try copying one of the spaces form a rich text field, and pasting it into the replaceAll. It may help. – Kasper Jun 23 '17 at 22:56
  • What do you mean? Because I'm actually sure I used the space bar to put the spaces on the Rich Text Area field.. not tab or anything else.. – Loren Jun 23 '17 at 23:05
  • 1
    I have checked that normalizeSpace() works well for richtextarea, if I put space using space bar it works well to – Santanu Boral Jun 23 '17 at 23:16
  • 1
    I tried it too on the EU6 instance, and it's working for me. It's a very basic function so i'm guessing it's unlikely to be broken. In the past I have had issues with white space characters that are encoded differently than the normal space. But if you've entered it manually that's not the issue here. – Kasper Jun 23 '17 at 23:18

Please, look at the "space" char codes of your textArea string.

  • I never thought that the space I'm seeing is a non-breaking space and what I'm entering is a regular space. So they have different character codes. [\\x{a0}\\s\\s] this regex worked for me. I got it checked by someone from regex101.com. – Loren Aug 13 '17 at 5:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.