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

Hi @Ankur Bawiskar ,

Please find below code which we are using to remove the html tags, we need to remove the special character as well when copied from html field to string field.

 

var gr3 = new GlideRecord("u_exception_request");
gr3.query();
while (gr3.next()) {

    var desc1 = gr3.u_short_description.replace(/<br \/>/g, '\n');
    desc1 = desc1.replace(/<(.|\n)*?>/gi, '');
    gr3.u_exception_short_description = desc1;
    gr3.update();

}

Hi @Sagar S ,

Try below script it will work.

var gr3 = new GlideRecord("u_exception_request");
gr3.query();
while (gr3.next()) {
    var htmlText = gr3.u_short_description;
    // Remove HTML tags
    var cleanedText = htmlText.replace(/<[^>]*>/g, '');
    // Remove special characters
    cleanedText = cleanedText.replace(/[^\w\s]/g, '');
    gr3.u_exception_short_description = cleanedText;
    gr3.update();
}

Thanks,

Anand

Hi @Anand Kumar P ,

 

Thanks for your reply. I tried for one record by using above script but it didn't work.

 

Regards,

Sagar

Hi @Sagar S ,

Try below script

 

var gr3 = new GlideRecord("u_exception_request");
gr3.query();
while (gr3.next()) {
    var htmlText = gr3.u_short_description;
    var get1=new GlideSPScriptable().stripHTML(htmlText);
var value1 = get1.replace(/[^a-zA-Z0-9]+/g, '-'); //
var value2 = value1.replace(/^-|-$/g, '');
    gr3.u_exception_short_description = value2;
    gr3.update();
}

Thanks,

Anand

Hi @Anand Kumar P 

 

It didn't work, attaching the screenshot. In the below screenshot exception short description is string field and short description is HTML field. As you can see # and & is still coming

short description.JPG