I’m working on a Zapier workflow that transfers customer information from an e-commerce platform to a video service. The automation runs smoothly most of the time, but it breaks when customer names contain special symbols like & or parentheses ( and ).
For instance, a name like “Mary & John (wedding)” causes the entire process to fail. I’m attempting to write a JavaScript code step in Zapier to clean up these problematic characters before passing the data forward.
Since I’m new to JavaScript coding, I keep running into syntax errors. Here’s what I’ve tried so far:
// wrapped in async function
// await can be used anywhere in the code
cleanName = inputData.customerName.replace(/[^a-zA-Z0-9 ]/g, '_').replace(/_{2,}/g, '_');
return {cleanedResult: cleanName};
The code should remove any non-alphanumeric characters (except spaces) and replace them with underscores, then clean up multiple consecutive underscores. However, I’m getting errors when testing this code block. What am I doing wrong with the syntax?
Others already caught the missing variable declaration, but there’s another issue you’ll hit. Zapier’s JavaScript environment gets weird with certain regex operations. I’ve had better luck breaking the replacement into separate steps.
Use const instead of let since you’re not reassigning the variable, and throw in a null check at the start. Your regex looks good, but you might want to keep hyphens and apostrophes - they show up in real names all the time. Try /[^a-zA-Z0-9 '-]/g instead.
Also, test this with actual Zapier data. The input object structure often differs from what you expect during development.
you gotta use let or const for that cleanName var. try this: let cleanName = inputData.customerName.replace(/[^a-zA-Z0-9 ]/g, '_').replace(/_{2,}/g, '_'); without that declaration, it won’t run right.
You need to declare your variable first. Use let, const, or var - JavaScript won’t work without it. Your code looks fine otherwise, but you should handle null or undefined inputs since they’ll throw errors. I’d add something like if (!inputData.customerName) return {cleanedResult: ''}; before the replacement logic. Your regex patterns look right for what you’re doing. You might want to trim leading or trailing underscores from the cleaned result - just chain .replace(/^_+|_+$/g, '') at the end.