Skip to Main Content
Programming ColdFusion MX, 2nd Edition
book

Programming ColdFusion MX, 2nd Edition

by Rob Brooks-Bilson
August 2003
Intermediate to advanced content levelIntermediate to advanced
1140 pages
68h 45m
English
O'Reilly Media, Inc.
Content preview from Programming ColdFusion MX, 2nd Edition

Useful Regular Expressions

Now that you have a better idea of how regular expressions work, let’s take a look at how you can actually use them in your own ColdFusion applications. Here are some regular expressions that perform common search-and-replace tasks:

  • Replace all characters in a string that aren’t numbers, letters, or underscores with an underscore:

    <cfset NewString = REReplaceNoCase("This is a test", "\W", "_", 
           "All")>
  • Replace all spaces in a filename with the underscore character; this is useful in situations where you allow users to upload files to your ColdFusion server via their web browsers:

    <cfset NewFilename = REReplace("My File Name.txt", " ", "_", "All")>
  • Return the full directory path from a string containing a full path including filename:

    <cfset TheDirectory = REReplaceNoCase(MyPath, "\w+\.\w+", "", "All")>
  • Return the filename from a string containing a full path including filename:

    <cfset TheFileName = REReplaceNoCase(MyPath, "([A-Z]:\\)|\w+\\+", "", "All")>
  • Remove all doubled words in a string:

    <cfset NoRepeats = REReplaceNoCase("I want to to go to the park.", 
      "\b([a-z]+)[ ]+\1","\1","All")>
  • Strip all HTML tags from a string; this is useful for removing HTML from form-field submissions:

    <cfset NoHTML = REReplace(MyString, "<[^>]*>", "", "All")>
  • Remove all HTML anchors in a string, leaving just the text, if any:

    <cfset NoHTMLLinks = 
      REReplaceNoCase(MyString, "<a [^>]*>([[:print:]]*)</a>","\1",'All')>
  • Format all email addresses in a string as HTML mailto: links. Note ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming ColdFusion

Programming ColdFusion

Rob Brooks-Bilson

Publisher Resources

ISBN: 0596003803Supplemental ContentErrata Page