As a follow up to my quick little regex post yesterday, I thought I'd share another one today. This is something I'm adding to BlogCFC later this week, but as I was working on Adobe Groups today I figured I'd test it out there first. It's a formatting trick used n many places, including Google+, and it's something so simple you probably don't even have to document it. What this code will do is convert any word surrounded with asterisks to bolded text and any word surrounded with underscores to italics. Here's the UDF:
<cfscript>
function simpleFormat(s) {
s = rereplace(s, "\*(\w+?)\*","<b>\1</b>","all");
s = rereplace(s, "_(\w+?)_","<i>\1</i>","all");
return s;
}
</cfscript>
And here is my sample text:
This is some text I feel *very* strongly about. Maybe _you_ don't feel strongly about it, but honestly, maybe you are just slow.* Or maybe you aren't *slow* per se but you could _kinda_ slow.
- = No offense to anyone who walks slow!
Notice I intentionally used a * as a pointer to a note at the end of the string. That was to test solitary characters. Here is the output: (Note - edited to remove the blockquote as it messed up formatting.)
This is some text I feel very strongly about. Maybe you don't feel strongly about it, but honestly, maybe you are just slow.* Or maybe you aren't slow per se but you could kinda slow. * = No offense to anyone who walks slow!
Not rocket science, and I know folks are going to say "what about lists, links, etc etc", but it's nice, simple, and an effective update to plain text. By the way, if you want to handle turning vertical spaces into paragraphs, you can simply add XHTMLParagraphFormat to the code as well.