A practical field guide from Automation Ace.
Parsing and Transforming Strings in Zapier JavaScript Code Steps
String data arriving in Zapier Code steps frequently needs transformation before it's usable: a full name must be split into first and last, a URL must be parsed for its query parameters, a long text must be truncated to fit a field limit, or a delimited string must be broken into individual parts. JavaScript's string methods handle all of these natively. This guide covers the essential string parsing patterns with examples ready for Zapier's Code step environment.
split(): Break a String on a Delimiter
split() divides a string at every occurrence of a delimiter and returns an array:
const text = inputData.text; // e.g. "apple, banana, cherry"
// Split on comma with optional surrounding space
const items = text.split(',').map(s => s.trim()).filter(s => s !== '');
// Split on newline (for multi-line text)
const lines = text.split('\n').map(s => s.trim()).filter(s => s !== '');
// Split on a specific word or phrase
const parts = text.split(' and ');
// Split into individual characters
const chars = text.split('');
output = {
items: items.join('|'), // rejoin for output
item_count: String(items.length),
first_item: items[0] || '',
last_item: items[items.length - 1] || ''
};
split() with a Limit
The second argument to split() limits how many parts are returned:
const text = 'one,two,three,four,five';
// Split into at most 2 parts (everything after the first comma stays in part 2)
const parts = text.split(',', 2); // ['one', 'two']
// To split on first delimiter only and keep the rest together:
const firstComma = text.indexOf(',');
const before = firstComma !== -1 ? text.substring(0, firstComma) : text;
const after = firstComma !== -1 ? text.substring(firstComma + 1) : '';
output = { before, after };
slice(): Extract a Substring by Position
slice(start, end) extracts characters from position start up to (but not including) end. Negative indices count from the end:
const text = inputData.text; // e.g. "Hello, World!"
// First 5 characters
const first5 = text.slice(0, 5); // "Hello"
// From position 7 to end
const fromPos7 = text.slice(7); // "World!"
// Last 6 characters
const last6 = text.slice(-6); // "orld!"
// Truncate to 100 characters with ellipsis
const truncated = text.length > 100 ? text.slice(0, 100) + '...' : text;
// Remove first and last character (e.g. strip surrounding quotes)
const stripped = text.slice(1, -1);
output = {
first5,
from_pos7: fromPos7,
truncated,
char_count: String(text.length)
};
Split Full Name into First and Last Name
Splitting a full name is trickier than it looks — names can have prefixes (Dr., Mr.), suffixes (Jr., III), middle names, and hyphenated surnames. Here are patterns from simple to robust:
const fullName = inputData.full_name.trim();
// Simple split on first space
const spaceIndex = fullName.indexOf(' ');
const firstName = spaceIndex !== -1 ? fullName.substring(0, spaceIndex) : fullName;
const lastName = spaceIndex !== -1 ? fullName.substring(spaceIndex + 1) : '';
output = { first_name: firstName, last_name: lastName };
// More robust: split on spaces, treat everything before last word as first name
const parts = fullName.split(' ').filter(p => p !== '');
let firstName, lastName;
if (parts.length === 0) {
firstName = '';
lastName = '';
} else if (parts.length === 1) {
firstName = parts[0];
lastName = '';
} else {
lastName = parts[parts.length - 1];
firstName = parts.slice(0, -1).join(' '); // "Mary Jane" stays together as first name
}
output = {
first_name: firstName,
last_name: lastName,
name_parts: String(parts.length)
};
Parse URL Query String Parameters
URLSearchParams is available as a global in Zapier JavaScript Code steps — the same object used to build query strings for GET requests can also parse them:
const url = inputData.url; // e.g. "https://example.com/page?utm_source=email&utm_medium=newsletter&id=123"
// Extract the query string portion
const queryString = url.includes('?') ? url.split('?')[1] : '';
// Parse into an object
const params = new URLSearchParams(queryString);
// Access specific parameters
const utmSource = params.get('utm_source') || '';
const utmMedium = params.get('utm_medium') || '';
const id = params.get('id') || '';
// Get all parameter names
const paramNames = [...params.keys()].join(',');
// Build an object of all params (for JSON output)
const allParams = {};
params.forEach((value, key) => { allParams[key] = value; });
output = {
utm_source: utmSource,
utm_medium: utmMedium,
id,
param_names: paramNames,
all_params: JSON.stringify(allParams),
has_query_string: queryString !== '' ? 'true' : 'false'
};
Common String Utility Patterns
const text = inputData.text;
// Trim whitespace from both ends
const trimmed = text.trim();
// Convert case
const lower = text.toLowerCase();
const upper = text.toUpperCase();
const titleCase = text.toLowerCase().replace(/\b\w/g, c => c.toUpperCase());
// Check if string contains a value
const contains = text.toLowerCase().includes(inputData.search.toLowerCase()) ? 'true' : 'false';
// Replace all occurrences of a string
const replaced = text.replaceAll('old', 'new');
// Or with regex for case-insensitive replace:
const replacedCI = text.replace(/old/gi, 'new');
// Remove all non-alphanumeric characters (slugify)
const slug = text.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '');
// Pad a number with leading zeros: "5" → "005"
const padded = String(parseInt(text, 10) || 0).padStart(3, '0');
output = { trimmed, lower, upper, title_case: titleCase, contains, slug, padded };
Extract a Value Between Two Delimiters
const text = inputData.text; // e.g. "Order #[ORDER-123] confirmed"
const start = '[';
const end = ']';
const startIdx = text.indexOf(start);
const endIdx = text.indexOf(end, startIdx);
const extracted = (startIdx !== -1 && endIdx !== -1)
? text.substring(startIdx + 1, endIdx)
: '';
output = { extracted, found: extracted !== '' ? 'true' : 'false' };
The string parsing patterns that come up most in Zapier Code steps:split(',')to break line-item strings,slice()to truncate or extract substrings,URLSearchParamsto parse URL query strings, andindexOf()/substring()to extract content between known delimiters. All of these are available natively — no libraries needed.
For array operations on the results of split, see JavaScript array methods in Zapier Code steps. For JSON parsing and number parsing, see parsing JSON and numbers in Zapier Code steps. For the full JavaScript environment reference, see JavaScript libraries in Zapier Code steps. For help with a string transformation workflow, talk to Automation Ace.
Disclaimer: This article may include links to apps, products, or services. Some links may be affiliate links, which means Automation Ace may earn a commission at no extra cost to you.