Splitting a field separates one value into several, such as a full name into first and last name, or Arlington, VA 22201 into city, state, and zip code. It is the reverse of concatenating, and it is harder, because the split has to guess where one part ends and the next begins.
Why it matters when importing data
Many exports put everything in one column, while your product needs structured fields to sort, filter, or send to another system. Splitting on import gives you the structure without asking customers to restructure their spreadsheets.
Splitting Fields in JavaScript
Use the most predictable delimiter available. For names, the last word is usually the last name. For a city line, a regular expression can pull out the state and an optional zip code:
const rows = [
{ name: "Ada Lovelace", cityLine: "London, UK" },
{ name: "Grace Brewster Hopper", cityLine: "Arlington, VA 22201" },
{ name: "Cher", cityLine: "Los Angeles, CA 90001-1234" },
];
// Everything before the last space is the first name; the rest is the last name.
const splitName = (name) => {
const parts = name.trim().split(/\s+/);
return parts.length === 1
? { firstName: parts[0], lastName: "" }
: { firstName: parts.slice(0, -1).join(" "), lastName: parts.at(-1) };
};
// "City, ST 12345" or "City, Country"
const splitCityLine = (line) => {
const match = line.match(/^(.+?),\s*([A-Z]{2})(?:\s+(\d{5}(?:-\d{4})?))?$/);
return match
? { city: match[1], region: match[2], postalCode: match[3] ?? "" }
: { city: line, region: "", postalCode: "" };
};
const results = rows.map((row) => ({
...splitName(row.name),
...splitCityLine(row.cityLine),
}));
console.table(results); Input
| name | cityLine |
|---|---|
| Ada Lovelace | London, UK |
| Grace Brewster Hopper | Arlington, VA 22201 |
| Cher | Los Angeles, CA 90001-1234 |
Output
| First name | Last name | City | Region | Postal code |
|---|---|---|---|---|
| Ada | Lovelace | London | UK | (blank) |
| Grace Brewster | Hopper | Arlington | VA | 22201 |
| Cher | (blank) | Los Angeles | CA | 90001-1234 |
Edge cases to handle
- Names are not uniform. Mononyms such as
Cher, compound surnames such asGarcía Márquez, and suffixes such asJr.do not follow first and last. Keep the original value too. - Delimiters inside values. A company called
Smith, Jones & Cobreaks a split on commas. Split on a pattern, not just a character. - Partial matches. When a value does not fit the pattern, keep it whole in the first field rather than dropping it, as the city split above does.
- Let people review. Splits are guesses. Show the result so users can correct the exceptions.
How Dromo handles splitting fields
In Dromo, a step hook adds the new fields with instance.addField, a row hook fills them from the source column, and instance.removeField can drop the original. For the opposite case, several source columns that belong in one field, manyToOne maps multiple columns to a single field.
Related terms
- Concatenating Fields: Combining two or more fields into one value.
- Finding Text Within a String: Locating a substring inside a value, and optionally replacing it.
- Trimming Fields: Removing unwanted whitespace from the start and end of a value.
Frequently asked questions
How do I split a full name into first and last name?
Split on whitespace, treat the last word as the last name and everything before it as the first name, and handle one-word names separately. Keep the original full name, because no rule handles every name correctly.
How do I split a string in JavaScript?
Use value.split(separator), which returns an array. For irregular spacing use a regular expression, such as value.trim().split(/\s+/), and use match with capture groups when you need specific parts.