Translate special characters from Rich Text field to Plain Text Field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2022 11:28 PM
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 ! @ # $ % ^ & * ( ) " '</p>
<p><strong><span style="font-size: 12pt;">2. Test for Special Character ! @ # $ % ^ & * ( ) " '</span></strong></p>
Please guide me on how this can be resolved.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2022 05:14 PM
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 ! @ # $ % ^ & * ( ) " '
2. Test for Special Character ! @ # $ % ^ & * ( ) " '
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(/(@)/g, "@");
desc = desc.replace(/(&)/g, "&");
desc = desc.replace(/(")/g, '"');
desc = desc.replace(/(')/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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-08-2024 05:22 AM
I've been looking everywhere for this, thank you Maik !