JavaScript Pattern Matching Issues in Zapier Automation

I have a JavaScript code block in my Zapier workflow that uses regular expressions to match strings from incoming webhook data. The code should identify different client names and assign corresponding contact details for email notifications.

The issue is that this code only works correctly about 25% of the time during testing. When I check the execution history, everything appears successful with no error messages, but the pattern matching seems unreliable. I can’t figure out what’s causing this inconsistent behavior.

Here’s my current implementation:

var pattern1 = new RegExp("demo");
var pattern2 = new RegExp("premium+");
var pattern3 = new RegExp("basic");
var pattern4 = new RegExp("Enterprise Corp");

if(pattern1.test(inputData.clientName)){
    output = {contactName: 'Sarah', contactEmail: '[email protected]'};
}
else if(pattern2.test(inputData.clientName))
{
    output = {contactName: 'Premium Team', contactEmail: '[email protected]'};
}
else if(pattern3.test(inputData.clientName))
{
    output = {contactName: 'Basic Support', contactEmail: '[email protected]'};  
}
else if(pattern4.test(inputData.clientName))
{
    output = {contactName: 'Enterprise Corp', contactEmail: '[email protected]'}; 
}

What could be causing this intermittent failure in pattern recognition?

I’ve hit this exact issue with Zapier webhooks before. The regex looks fine - it’s almost always the incoming clientName data that’s messy. Webhook payloads love to throw in extra whitespace, weird capitalization, or null values that don’t show up clearly in the logs. I’d start by logging the actual inputData.clientName value and its type before you run any pattern matching. Then add trim() and toLowerCase() to clean up the input before testing your patterns. Trust me, webhook data is never as clean as it looks in testing, and tiny formatting differences cause exactly what you’re seeing.

this screams data type issue. webhook payloads are inconsistent - clientName might come thru as a string, object, or undefined. drop console.log(typeof inputData.clientName) at the top and you’ll probably see it’s not always a string. zapier’s execution timing is wonky too, so wrap everything in try/catch to catch any silent errors.

Your regex pattern “premium+” is broken - the plus sign matches one or more ‘m’ characters, so you’ll get “premium”, “premiumm”, “premiummm” etc. To match a literal plus sign, escape it: “premium+”. Also, use case-insensitive flags with new RegExp(“demo”, “i”) since client names probably come in different cases. Those intermittent failures? Your input data’s changing in ways you haven’t caught yet. Add a fallback else clause to catch unmatched cases and log the actual input value - that way you can see what’s slipping through your patterns.