Translate special characters from Rich Text field to Plain Text Field

DK18
Kilo Contributor

Hello Everone,

We have two fields, one custom field (rich text) and the other out of box field (plain text). We have a requirement to transfer from the rich text to the plain text field.  The data is copied as HTML codes rather than the actual character.

Rich Text

1. Test for Special Character ! @ # $ % ^ & * ( ) " '

2. Test for Special Character ! @ # $ % ^ & * ( ) " '

Copied as

<p>1. Test for Special Character ! &#64; # $ % ^ &amp; * ( ) &#34; &#39;</p>
<p><strong><span style="font-size: 12pt;">2. Test for Special Character ! &#64; # $ % ^ &amp; * ( ) &#34; &#39;</span></strong></p>

Please guide me on how this can be resolved.

6 REPLIES 6

Thanks Maik for your detailed response. It makes sense, but the business problem is still unresolved.

We tried to use the regex to resolve this issue, which again gives a partial outcome. I'm not an expert with Regex, can you review this solution and see if there is an option to make it better.

Option 1: desc = desc.replace(/<\/?[^>]+(>|$)/g, "");

Rich Text

1. Test for Special Character ! @ # $ % ^ & * ( ) " '

2. Test for Special Character ! @ # $ % ^ & * ( ) " '

Plain Text

1. Test for Special Character ! &#64; # $ % ^ &amp; * ( ) &#34; &#39;
2. Test for Special Character ! &#64; # $ % ^ &amp; * ( ) &#34; &#39;

The line break is maintained and most of the characters are translated ok. However, @,&,",' didn't translate. To overcome, I included additional Regex for each characters like below

Option 2: 

desc = desc.replace(/(&#64;)/g, "@");
desc = desc.replace(/(&amp;)/g, "&");
desc = desc.replace(/(&#34;)/g, '"');
desc = desc.replace(/(&#39;)/g, "\'");

desc = desc.replace(/<\/?[^>]+(>|$)/g, "");

Rich Text

1. Test for Special Character ! @ # $ % ^ & * ( ) " '

2. Test for Special Character ! @ # $ % ^ & * ( ) " '

Plain Text

1. Test for Special Character ! @ # $ % ^ & * ( ) " '
2. Test for Special Character ! @ # $ % ^ & * ( ) " '

 

I don't like adding individual Regex, as the solution isn't fool proof. There will always be characters missed from the list. Let me know if there is a better option.

 

Thanks.

I've been looking everywhere for this, thank you Maik !