Sorcerer's IsleDocs cfRegexOverviewCode

Quote

The quote action will wrap a string in \Q and \E meaning it can be used within a regex without needing to escape individual metacharacters.

This can be an easier to read solution than the Escape action, but is less flexible (since you cannot quote \E itself). It is also not possible to use quote within character classes, whereas Escape can be used here also.

Object

Although it is possible to call quote for a regex object, it is rare that you will need to do this. The RegexQuote function is usually recommended instead.

Arguments

No arguments.

Usage Examples

<cfset ExampleRx = new Regex('\w+\s{2,}') />

<cfdump var=#ExampleRx.quote()# />
Outputs: '\Q\w+\s{2,}\E'

Tag

Since the cfregex tag always treats its body content as a regex, the contents must currently be a valid regex pattern, otherwise an error is thrown - even when using the quote action.

To convert arbitrary text that is not a regex pattern, use the RegexQuote function instead. (If necessary, in combination with cfsavecontent.)

Attributes

Name Type Required Default Notes
Variable VarName no "cfregex" The variable which the escaped regex is assigned to.

Usage Examples

<cfregex quote variable="RegexQuoted" >
    \w+
    \s{2,}
</cfregex>
<cfdump var=#RegexQuoted# />
Outputs: '\Q\w+\s{2,}\E'

Function

The RegexQuote function accepts the argument Text, not Pattern, since it does not compile to a regex pattern.

This means it is possible to use RegexQuote against a non-regex piece of text and make it safe for use in regex. (This is not possible using the cfregex tag with action quote, since the tag always compiles its contents to a regex.)

Arguments

Name Type Required Default Notes
Text String yes n/a

Usage Examples

<cfdump var=#RegexQuote('\w+\s{2,}')# />
Outputs: '\Q\w+\s{2,}\E'

<cfdump var=#RegexQuote('*\o/* :)')# />
Outputs: '\Q*\o/* :)\E'