REReplaceCallback(string, pattern, callback [, scope])
Last updated March 10, 2010
Version: 1 | Requires: ColdFusion 8 | Library: CFMLLib
Description:
With the built-in REReplace function you can only supply a static replace string (parameterized with backreferences, of course). This UDF allows you to supply a function callback that will be passed all matches (including subexpressions) allowing you to use arbitrary CFML to construct the replacement string dynamically.
Return Values:
Returns a string.
Example:
string = "The catapult bifurcated the bearcat.";
fancyString = REReplaceCallback(string, "(\w*)(cat)(\w*)", doit, "all");
function doit(match) {
if (match[2] EQ "") {
return '#match[2]#<b><i>#match[3]#</i></b>#match[4]#';
} else {
return '<u>#match[2]#<b><i>#match[3]#</i></b>#match[4]#</u>';
}
}
</cfscript>
Parameters:
| Name | Description | Required |
|---|---|---|
| string | String to parse. | Yes |
| pattern | Regex pattern to use. | Yes |
| callback | UDF to be used as a callback. | Yes |
| scope | How many replacements should be made. Defaults to one. | No |
Full UDF Source:
<!---
REReplaceCallback behaves like REReplace, except instead of supplying a replacement string, you supply a function to invoke on each match.
@param string String to parse. (Required)
@param pattern Regex pattern to use. (Required)
@param callback UDF to be used as a callback. (Required)
@param scope How many replacements should be made. Defaults to one. (Optional)
@return Returns a string.
@author Barney Boisvert (bboisvert@gmail.com)
@version 1, March 10, 2010
--->
<cffunction name="REReplaceCallback" access="private" output="false" returntype="string">
<cfargument name="string" type="string" required="true" />
<cfargument name="pattern" type="string" required="true" />
<cfargument name="callback" type="any" required="true" />
<cfargument name="scope" type="string" default="one" />
<cfscript>
var start = 0;
var match = "";
var parts = "";
var replace = "";
var i = "";
var l = "";
while (true) {
match = REFind(pattern, string, start, true);
if (match.pos[1] EQ 0) {
break;
}
parts = [];
l = arrayLen(match.pos);
for (i = 1; i LTE l; i++) {
if (match.pos[i] EQ 0) {
arrayAppend(parts, "");
} else {
arrayAppend(parts, mid(string, match.pos[i], match.len[i]));
}
}
replace = callback(parts);
start = start + len(replace);
string = mid(string, 1, match.pos[1] - 1) & replace & removeChars(string, 1, match.pos[1] + match.len[1] - 1);
if (scope EQ "one") {
break;
}
}
return string;
</cfscript>
</cffunction>
Search CFLib.org
Latest Additions
Alan McCollough added
forceBoolean
19 hour(s) ago
Shawn Porter added
DeMoronize
3 day(s) ago
Chris Carey added
readPropertiesFi...
4 day(s) ago
Randy Johnson added
lastDayofWeek
6 day(s) ago
Top Rated
QuickSort
Rated 5.0, 3 time(s)
indentXml
Rated 5.0, 3 time(s)
queryColumnsToSt...
Rated 5.0, 3 time(s)
generateSsccAsn
Rated 5.0, 3 time(s)