February 18, 2019
Convert a string To Title Case
Like
(5)
Comments
(3)
5
3
Summary
Convert a string To Title Case using Regular Expressions
A question came up on the CFML Slack channel today about converting a string to Title Case. I thought I’d see if I could solve it using a regular expression and this is what I came up with:
s = "iTSy bITSY TeeniE wEENIE Yellow pOLKADOT BiKiNi"; titleCase = REReplaceNoCase(s, "\b(\w)(\w+)\b", "\U\1\L\2", "all"); writeDump(titleCase); // Itsy Bitsy Teenie Weenie Yellow Polkadot Bikini
I tested this on ACF10, ACF11, ACF2016, ACF2018 and Lucee.