Splitting Fields

Separating the contents of one field into several fields.

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

namecityLine
Ada LovelaceLondon, UK
Grace Brewster HopperArlington, VA 22201
CherLos Angeles, CA 90001-1234

Output

First nameLast nameCityRegionPostal code
AdaLovelaceLondonUK(blank)
Grace BrewsterHopperArlingtonVA22201
Cher(blank)Los AngelesCA90001-1234

Edge cases to handle

  • Names are not uniform. Mononyms such as Cher, compound surnames such as García Márquez, and suffixes such as Jr. do not follow first and last. Keep the original value too.
  • Delimiters inside values. A company called Smith, Jones & Co breaks 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.

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.