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.
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.
No arguments.
<cfset ExampleRx = new Regex('\w+\s{2,}') />
<cfdump var=#ExampleRx.quote()# />
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.)
Name | Type | Required | Default | Notes |
---|---|---|---|---|
Variable | VarName | no | "cfregex" | The variable which the escaped regex is assigned to. |
<cfregex quote variable="RegexQuoted" >
\w+
\s{2,}
</cfregex>
<cfdump var=#RegexQuoted# />
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.)
Name | Type | Required | Default | Notes |
---|---|---|---|---|
Text | String | yes | n/a |
<cfdump var=#RegexQuote('\w+\s{2,}')# />
<cfdump var=#RegexQuote('*\o/* :)')# />