Pdf Processor
When your list-marker regex eats decimal numbers
TLDR
Ordered list marker stripping regular expressions using optional trailing whitespace (/^\d{1,3}[.)]\s/) accidentally match decimal numbers at the start of lines, corrupting technical text like "0.5 amp" into "5 amp". The bug occurs because optional whitespace (\s) matches zero trailing spaces, allowing "0." to satisfy the marker pattern. Adding a negative lookahead (?!\d) ensures numeric tokens containing decimals are preserved.
| Input Technical Copy | Defective Regex (/^\d{1,3}[.)]\s*/) | Remediated Regex ((?!\d)) | Output Accuracy |
|---|---|---|---|
"0.5 amp. maximum" | "5 amp. maximum" (Corrupted) | "0.5 amp. maximum" | 100% Preserved |
"1. Safety Instructions" | "Safety Instructions" (Stripped) | "Safety Instructions" | 100% Correct List |
Technical defect analysis
// DEFECTIVE REGEX: \s* allows zero whitespace, matching "0." inside "0.5"
const DEFECTIVE_ORDERED_MARKER_RE = /^(?:\d{1,3}[.)]\s|[a-zA-Z][.)]\s)/;
const text = "0.5 amp. maximum"; const stripped = text.replace(DEFECTIVE_ORDERED_MARKER_RE, ""); // Output: "5 amp. maximum" (DATA CORRUPTION!)
Remediation pattern
Incorporate negative lookahead (?!\d) on numeric branches and require explicit whitespace on letter branches:
// REFACTORED REGEX: Negative lookahead (?!\d) prevents decimal truncation
export const ORDERED_MARKER_STRIP_RE =
/^(?:\d{1,3}.)\s*|[a-zA-Z].)|[ivxIVX]+.))/;
export function stripListMarker(lineText) { return lineText.replace(ORDERED_MARKER_STRIP_RE, ''); }
Rule of thumb: Include negative lookahead assertions ((?!\d)) on prefix-stripping numeric regular expressions to prevent decimal token corruption.
Read this post in the full Engineering Journal →