View comments | RSS feed

REReplace

Description

Uses a regular expression (RE) to search a string for a string pattern and replace it with another. The search is case-sensitive.

Returns

If the scope parameter is set to one, returns a string with the first occurrence of the regular expression replaced by the value of substring.

If the scope parameter is set to all, returns a string with all occurrences of the regular expression replaced by the value of substring.

If the function finds no matches, it returns a copy of the string unchanged.

Category

String functions

Function syntax

REReplace(string, reg_expression, substring [, scope ])

See also

REFind, Replace, ReplaceList, REReplaceNoCase

History

ColdFusion MX: Added supports for the following special codes in a replacement substring, to control case conversion:

For more information on new features, see REFind.

Parameters

Parameter Description

string

A string or a variable that contains one. String within which to search.

reg_expression

Regular expression to replace. The search is case-sensitive.

substring

A string or a variable that contains one. Replaces reg_expression.

scope

  • one: replaces the first occurrence (default).
  • all: replaces all occurrences.

Usage

For details on using regular expressions, see Using Regular Expressions in Functions in ColdFusion MX Developer's Guide.

Example

<p>The REReplace function returns <i>string</i> with a regular expression replaced
with <i>substring</i> in the specified scope. Case-sensitive search. <p>REReplace("CABARET","C|B","G","ALL"): <cfoutput>#REReplace("CABARET","C|B","G","ALL")#</cfoutput> <p>REReplace("CABARET","[A-Z]","G","ALL"): <cfoutput>#REReplace("CABARET","[A-Z]","G","ALL")#</cfoutput> <p>REReplace("I love jellies","jell(y|ies)","cookies"): <cfoutput>#REReplace("I love jellies","jell(y|ies)","cookies")# </cfoutput> <p>REReplace("I love jelly","jell(y|ies)","cookies"): <cfoutput>#REReplace("I love jelly","jell(y|ies)","cookies")#</cfoutput>

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | KnowledgeBase | Bug Reporting

Version 7

Comments


tom.e said on Jan 18, 2006 at 9:18 AM :
Your example shows how to use the function, but it doesn't show what the results are. I would suggest changing it to:

---- begin results ----
The REReplace function returns string with a regular expression replaced with substring in the specified scope. Case-sensitive search.

REReplace("CABARET","C|B","G","ALL"): GAGARET

REReplace("CABARET","[A-Z]","G","ALL"): GGGGGGG

REReplace("I love jellies","jell(y|ies)","cookies"): I love cookies

REReplace("I love jelly","jell(y|ies)","cookies"): I love cookies

---- end results ----

Also, I would like to see an example with a replacement of what was matched; for example:

REReplace("I love jelly", "jell(y|ies)", "cook ")
becomes:
I love cooky.

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/7/htmldocs/00000614.htm