javascript
38 lines · 8 steps
Parsing full names into first and last
A name splitter that handles whitespace, suffixes, comma order, and multi-word surname particles.
Explained by
highlit
1function splitFullName(fullName) {
2 const cleaned = (fullName || '').trim().replace(/\s+/g, ' ');
3
4 if (!cleaned) {
5 return { firstName: '', lastName: '' };
6 }
7
8 const suffixes = /^(jr|sr|ii|iii|iv|md|phd|esq)\.?$/i;
9 const parts = cleaned.split(' ');
10
11 let suffix = '';
12 if (parts.length > 2 && suffixes.test(parts[parts.length - 1])) {
13 suffix = parts.pop();
14 }
15
16 if (parts.length === 1) {
17 return { firstName: parts[0], lastName: '', suffix };
18 }
19
20 if (cleaned.includes(',')) {
21 const [last, rest] = cleaned.split(',', 2).map((s) => s.trim());
22 return { firstName: rest, lastName: last, suffix };
23 }
24
25 const particles = /^(van|von|de|del|della|di|da|la|le|st\.?|mac|mc|o')$/i;
26 let splitIndex = parts.length - 1;
27 while (splitIndex > 1 && particles.test(parts[splitIndex - 1])) {
28 splitIndex -= 1;
29 }
30
31 return {
32 firstName: parts.slice(0, splitIndex).join(' '),
33 lastName: parts.slice(splitIndex).join(' '),
34 suffix,
35 };
36}
37
38export default splitFullName;
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Normalizing input first lets every later step assume clean, single-spaced text.
- 2Real-world names need special handling for suffixes, comma ordering, and surname particles like 'van' or 'de'.
- 3Returning a consistent object shape keeps callers simple no matter which branch runs.
Related explainers
javascript
const express = require('express'); const router = express.Router(); const { pool } = require('../db'); const redis = require('../redis');
Building a health check endpoint in Express
health-check
timeouts
promise-race
Intermediate
9 steps
java
public final class Slugifier { private static final Pattern NON_LATIN = Pattern.compile("[^\\w-]"); private static final Pattern WHITESPACE = Pattern.compile("[\\s]+");
Building a URL slugifier in Java
regex
unicode-normalization
string-processing
Intermediate
8 steps
javascript
'use client'; import { useEffect } from 'react'; import * as Sentry from '@sentry/nextjs';
How a Next.js error boundary recovers
error-boundary
error-handling
observability
Intermediate
8 steps
ruby
class ApacheLogParser LINE_PATTERN = /\A(?<ip>\S+)\s\S+\s\S+\s\[(?<time>[^\]]+)\]\s"(?<method>[A-Z]+)\s(?<path>\S+)\s(?<protocol>[^"]+)"\s(?<status>\d{3})\s(?<bytes>\d+|-)/ TIME_FORMAT = "%d/%b/%Y:%H:%M:%S %z"
Parsing Apache logs with named captures
regex
named-captures
parsing
Intermediate
6 steps
javascript
const MAX_BATCH_SIZE = 20; const FLUSH_INTERVAL = 5000; const ENDPOINT = '/api/analytics';
Batching analytics events in the browser
batching
buffering
sendbeacon
Intermediate
10 steps
javascript
export function diffIds(current, desired) { const currentSet = new Set(current); const desiredSet = new Set(desired);
Diffing sets to sync group membership
set-operations
diffing
transactions
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/parsing-full-names-into-first-and-last-explained-javascript-0b6a/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.