I’m working with Zapier and getting data that has missing values between commas. I need help with JavaScript to fill these gaps with empty strings.
Here’s what I’m getting from Zapier:
[email protected],,,[email protected],,[email protected]
What I want it to become:
[email protected],'','','',[email protected],'',[email protected]
Basically I need to convert consecutive commas into proper empty string values. I’m not great with JavaScript so any help would be awesome. The data comes from email fields and sometimes people leave blanks which creates these empty spots in the array.
I hit this same problem building a contact import system. Regex works but gets messy with multiple consecutive commas. Here’s what’s been rock solid for me: yourString.split(',').map(item => item.trim() === '' ? "''" : item).join(','). Split first, then map over the array to catch empty elements. Handles any number of consecutive empty fields plus whitespace-only entries that sneak through. Running this in production for months with zero issues.
Had the same problem with a CRM integration using Zapier webhooks. Found the cleanest fix was a simple replace with callback: data.replace(/(^|,)(?=,|$)/g, "$1''"). This catches empty spots at the start, middle, and end without splitting into arrays. The regex finds positions between commas or string boundaries and drops in empty quotes. Works great with multiple consecutive blanks and won’t break your existing data. Way more efficient than splitting large datasets too.
you can also use regex for this. try yourString.replace(/,,/g, "','"); - it’ll replace those empty gaps with quoted empty strings. looks much cleaner!