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

I tried this also but not able to achieve.


find_real_file.png



See ampersand symbol was not there and it was replaced


find_real_file.png


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);


Hi Pratik,

 

I have tested this out to remove HTML characters when submitted to a string field, however after submit i noticed there is an extra amount of white space, did you run into that same issue ? if so, do you know how to remove ?

find_real_file.png

Disregard, i was able to modify my code slightly to produce the desired results. Your answer was helpful in getting me there. Thank you. 

var a = current.description.getDisplayValue();
//to remove HTML characters.
a = a.replace(/&(lt|gt);/g, function (strMatch, p1){
return (p1 == "lt")? "<" : ">";
});
var b = a.replace(/<(?:.|\n)*?>/gm, '');
//to remove &amp;
var c = b.replace(/&amp;/g, '&');
var d = c.replace(/ /g, '');

current.setDisplayValue('description',d);