How can I add a space before each capital letter in a string with Coldfuion?

pcmelby
pcmelby used Ask the Experts™
on
Let's say the value of my string is "MyValueName". I'd like to to convert it to "My Value Name". My guess is this is possible using the REREPLACE function, but I'm not too familiar with Regular Expressions.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015
Commented:
<cfset str = ReReplace("MyValueName", "([A-Z])", " \1", "all")>
<cfoutput>#str#</cfoutput>
Most Valuable Expert 2015

Commented:
"([A-Z])"     : capture a single instance of an upper case letter
" \1"           : use backreference "\1" to replace the matched substring with itself plus a (space)

Author

Commented:
Thanks, aqx. That did. I just found this as well, which also works:

<cfset text = "MyValueName">
<cfset text = REReplace(text, "([.^[:upper:]])", " \1","all")>
<cfoutput>
#text#
</cfoutput>

Author

Commented:
Perfect, thanks.
Most Valuable Expert 2015

Commented:
>> REReplace(text, "([.^[:upper:]])", " \1","all")>

I like the POSIX version. The "upper" is slightly more intuitive :)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial