I’m working on an automation that transfers customer information from an ecommerce platform to a video service through Zapier. The workflow has been running smoothly for months, but I keep running into issues when customer names contain unusual characters like ampersands or brackets. Names such as Tom & Jerry (adventures) cause the entire process to fail because of the & symbol and the () brackets.
I decided to try using Zapier’s Code by Zapier feature to clean up these problematic characters before sending the data, but I’m getting syntax errors. I’m pretty new to JavaScript so I’m not sure what I’m doing wrong.
Here’s the code I’m trying to use:
// running inside an async function
// await can be used anywhere in this function
cleanName = inputData.cleanName.replace(/[^a-zA-Z0-9 ]/g, '-').replace(/-{2,}/g, '-');
output = [cleanName];
Can someone help me figure out why this isn’t working? I just need to replace any special characters with underscores or dashes to prevent the integration from breaking.
Had the same issue with special characters breaking Zapier workflows last year. You need to declare your variable properly and format the output so Zapier can handle it. Try this:
Main changes: use const to declare the variable and wrap your output in an object within an array. Your regex will also collapse multiple special characters into a single dash, which is usually what you want. This approach works reliably for cleaning customer data before sending it to external APIs through Zapier.
you gotta declare cleanName with let or const, else it won’t work. also, change output to an object like output = [{cleanName: cleanName}] so it matches what zapier expects.
Your regex looks good, but there’s another issue beyond the variable declaration others mentioned. The double replacement might cause weird behavior in edge cases. I’ve found it’s more reliable to handle this in one pass:
This targets the specific characters that usually break API integrations instead of using the broad negation pattern. I also added whitespace normalization since multiple spaces can mess things up too. Been using this for ecommerce data cleanup for two years without any failures.