Most 'Title Case' Buttons Are Wrong: What Chicago and AP Actually Require
Chicago writes Gone with the Wind, AP writes Gone With the Wind. Four title-case styles, and the regex that splits XMLHttpRequest correctly.
Is it Gone with the Wind or Gone With the Wind? Both are correct. Chicago style lowercases with; the Associated Press capitalizes it. Same four words, two different right answers, decided entirely by which style guide you follow.
Almost every online "Title Case" button ignores this. It uppercases the first letter of every word and stops there, so it prints Gone With The Wind — which is neither Chicago nor AP. That output is Capitalize Each Word, a different operation that just happens to resemble title case on short, preposition-free strings.
The rule most tools skip
Title case — the kind used for book titles, headlines, and headings — keeps small connecting words lowercase unless they open or close the title. The categories that go lowercase:
- Articles:
a,an,the - Coordinating conjunctions:
and,but,or,nor,for… - Short prepositions:
of,to,in,on,at,by…
The first and last word are always capitalized regardless of category. To Kill a Mockingbird — To capitalized because it leads, a lowercase. Of Mice and Men — Of capitalized because it leads, and lowercase. Two more titles you can check by hand: A Tale of Two Cities keeps of lowercase but capitalizes the leading A, and The Lord of the Rings lowercases both of and the interior the. Run either through naive "capitalize each word" and you get the wrong The Lord Of The Rings.
Where the style guides disagree is the threshold for prepositions and a couple of conjunctions:
- Chicago (CMOS 17 §8.159): every preposition is lowercase, no matter how long.
- AP: prepositions of four letters or more are capitalized.
So over (four letters) separates them: The Quick Brown Fox Jumps over the Lazy Dog in Chicago versus …Jumps Over the Lazy Dog in AP. The with in Gone with/With the Wind works the same way. And so and yet split the other direction: Chicago capitalizes them, while AP and MLA keep them lowercase.
One table, four styles
We encode each style as data, not as branching logic:
const TITLE_STYLES = {
chicago: {
prepositions: "all-lower",
// so / yet capitalize in Chicago → not in the set
alwaysLower: new Set(["a", "an", "the", "and", "but", "for", "nor", "or", "as"]),
},
ap: {
prepositions: { maxLowerLen: 3 },
alwaysLower: new Set(["a", "an", "the", "and", "but", "or", "for", "nor", "so", "yet", "as"]),
},
// apa, mla, simple …
};
The decision for each word collapses into three questions. Is it in alwaysLower? Lowercase it. Is it a preposition? Lowercase it, unless the style sets a length threshold and the word is over it. Otherwise capitalize. First word, last word, and the first word after a colon (a subtitle) are always capitalized. Notice that so and yet are simply absent from Chicago's set — so they capitalize — and present in AP's. Adding APA or MLA is a new row of data, never a new code path.
One deliberate limitation: a word that already contains an uppercase letter — iPhone, NASA, macOS — is left untouched, because lowercasing it and then re-capitalizing the first letter would wreck it.
XMLHttpRequest, and the regex that eats its own boundary
The programmer formats — snake_case, kebab-case, camelCase, and the rest — all run off one tokenizer that splits an identifier into lowercase words. The interesting input is XMLHttpRequest. The correct snake_case is xml_http_request, because XML is a single token. Naive splitters produce x_m_l_http_request, treating every capital as its own word.
We support both, because that is exactly what the "Detect acronyms" toggle controls. With acronyms off, the result genuinely should be x_m_l_http_request. The obvious way to write "insert a break before every capital that follows a letter" is a consuming regex with capture groups:
s.replace(/([A-Za-z])([A-Z])/g, "$1 $2"); // looks right, is wrong
On XMLHttpRequest this produces x_ml_http_request. The bug is that regex replacement is non-overlapping. Matching XM consumes both characters, so the next search starts at L — and the M–L boundary is never even examined. In a run of capitals, every other boundary gets swallowed.
The fix is zero-width lookaround. Assertions match positions, not characters, so nothing is consumed and adjacent boundaries all fire:
s.replace(/(?<=[A-Za-z])(?=[A-Z])/g, " "); // → x_m_l_http_request ✓
Acronym detection uses two assertions instead of one:
s.replace(/(?<=[a-z0-9])(?=[A-Z])/g, " ") // fooBar → foo · Bar
.replace(/(?<=[A-Z])(?=[A-Z][a-z])/g, " "); // XMLHttp → XML · Http
The second one encodes the actual linguistic rule: in a run of capitals followed by a lowercase letter, the last capital belongs to the next word. In XMLHttp, the H (uppercase, then a lowercase t) starts Http, so the break lands in front of it: XML | Http. No dependency, about six lines, O(n) in one pass.
Three smaller traps
ß is a one-way door. Uppercasing the German straße gives STRASSE — the standard mapping turns one letter into two. Lowercasing STRASSE gives strasse, not straße. So uppercase-then-lowercase is not a round trip, and the UI never claims it is.
Turkish i. Under a Turkish locale, istanbul uppercases to İSTANBUL (dotted capital I), not ISTANBUL. That is correct for prose — and the exact reason the identifier tokenizer forces ASCII lowercasing instead. Under Turkish rules ID would lowercase to ıd (dotless), quietly corrupting a variable name. The casing locale applies to the writing formats; the code formats stay ASCII.
Counting spaces in aLtErNaTiNg. Hello World becomes hElLo wOrLd. The trick is which characters get counted. Alternate on letters only, and W lands on letter-index 5 (odd → uppercase), giving the wrong hElLo WoRlD. Counting by absolute code-point index — where the space occupies a slot too — pushes W to index 6 (even → lowercase). Iterating with [...text] rather than charAt keeps emoji and CJK characters intact along the way.
Try it
Paste XMLHttpRequest into the case converter and watch snake_case read xml_http_request; switch off "Detect acronyms" and it becomes x_m_l_http_request. Then type gone with the wind and flip the Title Case style from Chicago to AP — with capitalizes itself. All 14 formats compute in a single pass.