Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hi Shishir,



For & -----&amp; is getting populated in plain text field.


So i have used &amp; and i also tried using   &amp still same error



find_real_file.png


Tested this in background script and it worked, not sure what could be the issue then



var b = '+test +&amp;'


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


gs.print(c);


Hi


Please try below line and let me know if still you are facing same issue.


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



Thanks


Hi Prakash,



i tried   var c = b.replace(/&amp/i, '&');   this is also not working for me.


Hi


Please refer to below link for regular expression, it may be helpful :


JavaScript RegExp Object



Thanks