To convert HTML character to plain text

pratik_kumar
Giga Expert

Hi All,

I have a requirement to create a business rule which should copy plain text from Html field("field name: unit_description") and update it to a text filed(field name :"u_so_description")

I created this business rule

(function executeRule(current, previous /*null when async*/) {

var a=current.unit_description.getXHTMLValue();

//to remove HTML text...

a= a.replace(/&(lt|gt);/g, function (strMatch, p1){

return (p1 == "lt")? "<" : ">";

});

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

current.setDisplayValue('u_so_description',b);

})(current, previous);

This business rule is replacing all symbols except &.

For & still i am getting &amp; in plain text.

Kindly help me on this/

1 ACCEPTED SOLUTION

pratik_kumar
Giga Expert

Using var c=b.replace(/&amp;/g, '&'); my issue got fixed.


Thanks all for your help



(function executeRule(current, previous /*null when async*/) {




var a=current.unit_description.getXHTMLValue();


//to remove HTML characters.


a= a.replace(/&(lt|gt);/g, function (strMatch, p1){


return (p1 == "lt")? "<" : ">";


});


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


//to remove &amp;


var c=b.replace(/&amp;/g, '&');


current.setDisplayValue('u_so_description',c);




})(current, previous);


View solution in original post

14 REPLIES 14

Shishir Srivast
Mega Sage

Please check if this helps:



(function executeRule(current, previous /*null when async*/) {


var a=current.unit_description.getXHTMLValue();


//to remove HTML text...


a= a.replace(/&(lt|gt);/g, function (strMatch, p1){


return (p1 == "lt")? "<" : ">";


});


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


var c = b.replace(&amp, '&');


current.setDisplayValue('u_so_description',c);


})(current, previous);


Still i am getting error



find_real_file.png


Hi Pratik,



Can you check the below link and try it. I have tried for getting the knowledge text in the background script and it is working fine. Can you check it and let us know if it helped?



How to get raw text from HTML field?


Please check if this helps:



var c = b.replace('&amp', '&');