Parsing WhatsApp chat exports: 10 format traps from real files (with the regexes that survive them)
I build WaChat2PDF and the free viewer WaChatViewer. Both parse WhatsApp's "Export chat" files, and this is what that parser learned from thousands of real exports in many languages. WhatsApp's export looks trivial. One
I build WaChat2PDF and the free viewer WaChatViewer. Both parse WhatsApp's "Export chat" files, and this is what that parser learned from thousands of real exports in many languages.
WhatsApp's export looks trivial. One line per message: a timestamp, a name, a colon, the text. The first regex everyone writes is something like this:
const line = /^(\d{1,2})\/(\d{1,2})\/(\d{2,4}), (\d{1,2}):(\d{2}) - ([^:]+): (.*)$/;
It works on your own phone's export, then fails on the first file from someone in another country. There is no published spec: the header follows the phone's language, region and OS, and changes between app versions. Here are the traps we hit, roughly in order of how often they cause wrong output.
1. Android and iOS use different headers
12/03/2024, 14:20 - Ana: See you at 6 β Android
[12/03/2024, 14:20:05] Ana: See you at 6 β iOS
iOS wraps the timestamp in brackets, adds seconds and drops the - separator. A parser has to accept both terminators: - (Android) or ] (iOS).
2. You can't tell DD/MM from MM/DD on one line
03/04/2024 is 3 April in most of the world and March 4 in the US. A single line can't tell you which. The whole chat can: if any header has a first field over 12, the chat is day-first; if any has a second field over 12, it's month-first.
function inferDateOrder(lines) {
let dmy = false, mdy = false;
for (const line of lines) {
const m = line.match(/^\[?(\d{1,4})[/.-](\d{1,2})[/.-](\d{1,4})/);
if (!m || +m[1] >= 1000) continue; // YYYY/MM/DD needs no guessing
if (+m[1] > 12) dmy = true;
if (+m[2] > 12) mdy = true;
if (dmy && mdy) break;
}
return mdy && !dmy ? 'MDY' : 'DMY';
}
Decide once per chat, then parse every line with that order. Deciding per line gives you messages that jump months backwards and forwards.
3. Years come in three shapes, and one of them is in the future
Two-digit years (12/03/24), four-digit years at the end, and year-first dates in Chinese, Japanese and Korean locales (2024/3/12). Thai phones may use the Buddhist calendar, so the year reads 2567. If a parsed year is above 2400, subtract 543.
4. The time isn't always HH:MM, and AM/PM moves around
Real headers we've seen:
12/03/2024 Γ 14:20 - Ana: β¦ French: "Γ " between date and time
12.3.2024 klo 14.20 - Ana: β¦ Finnish: "klo", and a dot in the time
12/03/24 14.20 - Ana: β¦ Indonesian: dot in the time
3/12/24, 2:20 PM - Ana: β¦ US: 12-hour clock
2024. 3. 12. μ€ν 2:20 - Ana: β¦ Korean: AM/PM word BEFORE the time
2024/3/12 δΈε2:20 - Ana: β¦ Chinese: same, no space
12/10/2026 9:15 da manhΓ£ - Ana: β¦ Portuguese: part of day AFTER the time
Other connectors between date and time include a las (Spanish), Γ s (Portuguese), um (German), alle (Italian), saat (Turkish) and Π² (Russian). AM/PM shows up as AM, a.m., a. m., μ€μ /μ€ν, δΈε/δΈε, εε/εεΎ and Arabic Ψ΅/Ω
.
The Portuguese one is nasty. A loose regex happily matches 12/10/2026 9:15 and reads da manhΓ£ - Ana as the sender name. Every message in the chat then belongs to a person called "da manhΓ£ - Ana". We now route those lines through a dedicated pattern before anything else:
const PT_DAY_PART_RE = /^\[?\d{1,4}[/.-]\d{1,2}[/.-]\d{1,4},?\s+\d{1,2}[:.]\d{2}(?:[:.]\d{2})?\s*da\s+(?:manh[Γ£a]|madrugada|tarde|noite)(?:\]|\s-\s)/i;
madrugada and manhΓ£ mean AM; tarde and noite mean PM.
5. Newer exports put a narrow no-break space before AM/PM
Recent exports write 2:20\u202FPM, using U+202F (narrow no-break space), not a normal space. It's invisible in an editor, and \s in JavaScript does match it, but if your regex uses a literal space (" PM") it silently fails on these files.
6. Invisible characters at the start of lines
Several characters can sit in front of the timestamp:
-
U+FEFF(byte-order mark) on the very first line. -
U+200E(left-to-right mark): iOS puts it on attachment lines. -
U+200F(right-to-left mark) andU+061C(Arabic letter mark) in RTL locales, sometimes inside the date. - Bidi isolates
U+2066βU+2069around names.
^\[?\d fails on all of these, and the line gets glued onto the previous message. Strip them before matching:
const LEADING_INVISIBLES_RE = /^[\uFEFF\u200E\u200F\u061C\u202A-\u202E\u2066-\u2069]+/;
7. Arabic and Persian digits
Arabic-locale exports can write the whole header in Arabic-Indic digits, with an Arabic comma and RLM marks inside: Ω‘Ω’β/Ω£β/Ω’Ω Ω’Ω€Ψ Ω’:Ω’Ω Ω
-. Convert to ASCII digits for matching only, and keep the message text exactly as written:
const probe = line.slice(0, 64)
.replace(/[\u0660-\u0669]/g, d => String(d.charCodeAt(0) - 0x0660)) // Arabic-Indic
.replace(/[\u06F0-\u06F9]/g, d => String(d.charCodeAt(0) - 0x06F0)) // Persian
.replace(/\u060C/g, ',') // Arabic comma
.replace(/[\u200E\u200F\u061C]/g, '');
Because the probe dropped characters, map the matched length back onto the original line before you cut the header off, or you'll chop the first letters of the message.
8. Messages that start with a date
Multi-line messages are normal: every line that doesn't start a new message belongs to the previous one. But people also paste lines like 12/03/2024 meeting moved. If your "is this a new message?" test is just ^\d+/\d+/\d+, that line becomes a fake message with a weird sender.
The fix: require the full header including its terminator (- on Android, ] on iOS) before treating a line as a new message.
9. Sender vs system message
After the header, split on the first ": ". No colon means a system line: "Ana added Ben", "Messages and calls are end-to-end encryptedβ¦", "You deleted this message". These are localized too, so don't detect them by text. Detect them by shape.
Two practical consequences:
- Don't use the first line as the chat title. It's usually the encryption notice, so a file named after it ends up called "Messages_and_calls_are_end_to_end_encryptedβ¦".
-
Names can contain colons in rare cases. Splitting on the first
": "is still the best rule, but keep it in mind when a contact is saved as "Work: Ana".
10. Media markers are localized, and iOS and Android differ
IMG-20240312-WA0007.jpg (file attached) β Android, with media
<attached: 00000012-PHOTO-2024-03-12-14-20-05.jpg> β iOS, with media
<Media omitted> β exported without media
All of them are translated. We've seen (archivo adjunto), (file allegato), (arquivo anexado), (fichier joint), and omitted-media variants such as <Multimedia omitido>, <MΓdia oculta>, <MΓ©dias omis>, <Medien ausgeschlossen>. iOS may also prefix the marker with a document title and page count: Offer.pdf β’ 3 pages <attached: β¦>. The "edited" marker is localized as well: <This message was edited>, <Mensagem editada>, <Ce message a Γ©tΓ© modifiΓ©>, and so on.
Match the structure first (angle brackets around a short label and a filename, or a filename followed by a parenthesized marker), then check the filename against the files actually present in the ZIP. A user can type <attached: x.jpg> in a message; only a real file in the archive makes it an attachment.
The approach that held up
-
Normalize first, parse second. Strip invisibles, convert digits, rewrite every known locale header into one canonical shape (
D/M/Y, H:MM[:SS] [AM|PM] -), then run one strict regex. - Decide date order per chat, not per line.
-
Require the terminator (
-or]) to start a new message. - Treat attachments as claims to verify against the ZIP's contents.
- Collect anonymous failures. We record the shape of headers that don't parse (digits and letters masked, never the text), which is how most of the formats above were found.
If you're building something on WhatsApp exports, test with exports from phones set to other languages: Korean, Arabic, Brazilian Portuguese with a 12-hour clock, and a US phone. Those four will break most first versions.
If you just need to read or convert a chat, the free WaChatViewer opens exports in the browser, and WaChat2PDF turns them into PDF.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.