How to replace special character in string field

Sagar S
Tera Contributor

Hi,

We have HTML field called "short description" and string field called "exception short description", we are copying the values from html field to string field by removing the html tags. now we are facing an issue that special characters is also copying from html field to string field. We would need to remove the special character from the string field and the special character should not copy from html field to string field.

 

Regards,

Sagar

1 ACCEPTED SOLUTION

Hi @Sagar S ,

In which script you are populating it.

I have created business rule with below script

AnandKumarP_0-1698925636944.png

 

AnandKumarP_1-1698925719698.png


Thanks,

Anand

View solution in original post

15 REPLIES 15

Anup Desai1
Mega Sage

Hi @Sagar S ,
Please refer this script from this article - Remove html tags when copying into a string field

 

var desc='<p>test1</p>';
gs.print('Original '+desc);

desc= desc.replace(/<\/?[^>]+(>|$)/g, "");
gs.print('html sanitized:'+desc);

 

Please mark the answer correct/helpful based on Impact.
Regards, Anup

Anand Kumar P
Giga Patron
Giga Patron

Hi @Sagar S ,

AnandKumarP_1-1698904785297.png

 

 

 

var yourHtmlStr='<p>html tah@*(@())</p>';
var get1=new GlideSPScriptable().stripHTML(yourHtmlStr);
var value1 = get1.replace(/[^a-zA-Z0-9]+/g, '-'); //
var value2 = value1.replace(/^-|-$/g, '');
gs.print(value2);

 

Please mark it as solution proposed and helpful if it serves your purpose.

Thanks,

Anand

Dipen Wadhwana
Giga Guru

Hi @Sagar S,

 

Can you please try below script:

// Replace special characters in a string field
var fieldValue = "yourStringField"; // Replace 'yourStringField' with the actual field name
var replacedValue = fieldValue.replace(/[^a-zA-Z0-9]/g, '_'); // Replace non-alphanumeric characters with underscores
current.yourStringField = replacedValue; 

Please mark the answer correct if it helped you.

 

Thanks,

Dipen

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Sagar S 

share what script are you using to remove the tags?

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader