I have a JavaScript code block in my Zapier workflow that uses regular expressions to match incoming webhook data and assign output values accordingly. The code runs without throwing any errors and shows green status in the execution history, but it only works correctly about 25% of the time during testing.
The script should match different client names from the webhook payload and set corresponding contact details for the following email step. However, the pattern matching seems unreliable even though the logic appears straightforward.
Here’s my current implementation:
var pattern1 = new RegExp("demo");
var pattern2 = new RegExp("premium+");
var pattern3 = new RegExp("basic");
var pattern4 = new RegExp("Elite Services");
if(pattern1.test(inputData.customer)){
output = {contactName: 'John', contactEmail: '[email protected]'};
}
else if(pattern2.test(inputData.customer))
{
output = {contactName: 'Premium Team', contactEmail: '[email protected]'};
}
else if(pattern3.test(inputData.customer))
{
output = {contactName: 'Basic Support', contactEmail: '[email protected]'};
}
else if(pattern4.test(inputData.customer))
{
output = {contactName: 'Elite Services', contactEmail: '[email protected]'};
}
What could be causing this inconsistent behavior in the regex matching?
I’ve hit this exact same issue with Zapier code steps. It’s almost always null or undefined inputs that aren’t handled properly. Your regex looks solid, but add some defensive checks first. Wrap your pattern matching like this: if(inputData.customer && typeof inputData.customer === 'string') before running the tests. Webhooks love sending empty strings, nulls, or random numbers depending on what system they’re coming from. When I dealt with this intermittent matching problem, turns out 75% of my webhook calls had clean string data, but the other 25% had busted or missing customer fields that just silently broke the regex.
yeah, that premium+ regex might be the problem. it looks for ‘premiu’ and then one or more 'm’s. u should use /\bpremium\b/i for word boundaries instead, or just includes() if u want it simpler.
Your regex patterns are the problem - they’re case-sensitive by default. If your webhook sends ‘Demo’ or ‘BASIC’ instead of lowercase, nothing matches. I ran into this exact issue with my Zapier workflows.
Two ways to fix it:
Add the case-insensitive flag: new RegExp("demo", "i") - the ‘i’ makes it ignore case.
Convert input to lowercase first: inputData.customer.toLowerCase() before testing. This works better than regex flags in some Zapier setups.
Also throw in a fallback else clause for unmatched cases, or your output variable will be undefined.