How to allow spaces before and after email IDs using regex expression?

Santhosh15
Tera Guru

Hello All,

 

I need some help from you.

I have a field called "u_email_addresses_for_targeted_email", I am adding some email id's with spaces and which is not sending emails whenever there is a space before or after the email id's.

 

Below is the script which is using in script include and calling on business rule.

 
parm4 = current.u_email_addresses_for_targeted_email.toString();
parm4 = parm4.replace( new RegExp( "[\r\n]", "gm" ), "," );
 
Please help me on this issue.
 
Thank you in Advance
 
 
10 REPLIES 10

Community Alums
Not applicable

Hi @Santhosh15 ,
To allow spaces before and after email IDs while ensuring the email addresses are valid, you can modify your script to trim the spaces before and after each email address. Here's how you can do it:

updates script

 

// Get the email addresses from the field
parm4 = current.u_email_addresses_for_targeted_email.toString();

// Replace newlines with commas and trim spaces around email addresses
parm4 = parm4
    .replace(new RegExp("[\r\n]", "gm"), ",") // Replace newlines with commas
    .split(",") // Split the string into an array of email addresses
    .map(email => email.trim()) // Trim spaces before and after each email address
    .join(","); // Join the array back into a comma-separated string

// Now parm4 will have email addresses with no spaces before or after them

 

If the input is:

"  user1@example.com , user2@example.com  \n user3@example.com "

The output will be:

 
"user1@example.com,user2@example.com,user3@example.com"

 

Community Alums
Not applicable

Hi @Santhosh15 ,

also you ca try this script

parm4 = current.u_email_addresses_for_targeted_email.toString();

// Replace new lines with commas
parm4 = parm4.replace(/\r?\n/g, ',');

// Trim spaces before and after each email
parm4 = parm4.split(',')
    .map(email => email.trim())  // Remove spaces before and after
    .filter(email => email)      // Remove empty values
    .join(',');

Ankur Bawiskar
Tera Patron
Tera Patron

@Santhosh15 

why would someone enter spaces in email address? ideally you should not allow by doing some validation on UI

are you setting recipient via eventQueue in Business rule and before triggering event removing the spaces

try this script

parm4 = current.u_email_addresses_for_targeted_email.toString();
parm4 = parm4.replace(/\s/g, '');
gs.info(parm4);

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Yes @Ankur Bawiskar , I am setting recipient via eventQueue in Business rule.

 

This is the script include

 getRecipients: function ( current ) {
        var parm3 = '';
        var parm4 = '';
 
        if ( current.u_publish_to_outage_portal_email_ta_dl == true ) {
            parm3 = current.u_users_for_targeted_email.toString();
            parm4 = current.u_email_addresses_for_targeted_email.toString();
            parm4 = parm4.replace( new RegExp( "[\r\n]", "gm" ), "," );
        }

        var final_parm1 = parm1.toString() + "," + parm3.toString();
        var final_parm2 = parm2.toString() + "," + parm4.toString();

        var obj = {};
        obj.final_parm1 = final_parm1;
        obj.final_parm2 = final_parm2;
        return obj;
    },
 
This is BR:
 
var answerObj = new ChangeOutageNotificationUtils().getRecipients(current);

    var total_users = answerObj.final_parm1.concat(answerObj.final_parm2);

    if (current.u_publish_to_outage_portal == true || current.u_publish_to_outage_portal_email_ta_dl == true) {
        gs.eventQueue('notify.chg.outage.scheduled.en', current, answerObj.final_parm1.toString(), answerObj.final_parm2.toString());
       // new NutTranslationsUtils().sendNotifications(total_users.split(','), 'notify.chg.outage.scheduled.', current);
        current.u_outage_scheduled_email_sent = 'true';
        current.update();
    } else {
        return false;
    }