Regular Expression Tester
Writing a regular expression is often a trial-and-error process. The process typically goes something like this:
Create a ColdFusion template that contains a string and a regular expression to perform the task you want.
Save the ColdFusion template.
Open your web browser, point it to the ColdFusion template, and see if the regular expression behaves as intended.
If the regular expression is successful, great! If not, you then have to modify the regular expression in the template and repeat Steps 2 through 4.
Some regular expressions are simple and can be worked out relatively quickly. Others, however, are more complicated and require you to repeat the trial-and-error process over and over. Constantly having to switch from the browser to the development environment can be a real pain.
With this in mind, let’s look at a ColdFusion-based
solution for building search-and-replace regular expressions. The
template in Example 18-1, called
expressiontester.cfm, creates a form for
collecting information about the regular expression you want to test
and uses the information to dynamically generate REReplace(
)
or REReplaceNoCase( )
code.
Example 18-1. Regular-expression tester
<cfparam name="form.Operation" default="REReplace"> <cfparam name="form.MyString" default=""> <cfparam name="form.MyRegex" default=""> <cfparam name="form.MySubString" default=""> <cfparam name="form.MyScope" default="All"> <cftry> <cfif form.Operation is "REReplace"> <cfset MyOutput=REReplace(form.MyString, ...
Get Programming ColdFusion MX, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.