How to allow spaces before and after email IDs using regex expression?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2025 08:24 PM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2025 08:29 PM
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"

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2025 08:30 PM
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(',');
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2025 08:47 PM
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.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-04-2025 09:17 PM - edited 02-04-2025 09:19 PM
Yes @Ankur Bawiskar , I am setting recipient via eventQueue in Business rule.
This is the script include