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
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react'; const cache = new Map(); const inflight = new Map();
Building a stale-while-revalidate hook in React
caching
request-deduplication
custom-hooks
Advanced
10 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
javascript
import { useEffect, useRef, useState } from 'react'; export function useDelayedFlag(active, delay = 300) { const [visible, setVisible] = useState(false);
Delaying a loading spinner with a React hook
custom-hooks
debouncing
cleanup
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.