webCOMAND

#REGEXREPLACE

Find and replace characters in a string based on a regular expression.

Prototype

#REGEXREPLACE($pattern, $replacement, $subject)

Parameters

  • pattern - A PCRE pattern to match on.  It can be a variable or string containing the PCRE, including the leading and trailing delimiter and optional modifier or a bare PCRE with a leading and trailing delimiter and optional modifier.
  • replacement - The string to replace matched patterns with.  It can include variables $1 .. $99 to reference text captured by the pattern, and $0 references text matched by the entire pattern.  To use a backslash, it must be escaped and doubled ("\\\\" string).
  • subject - The input string to match against.
To produce a single backslash from the replacement, "\\\\" must be used.  The string is first processed by cScript, then by the internal regex processor.  For example, "\\\\$1" will be passed to preg_repace() as "\\$1" and resolve to "\<captured text>".

Return

Returns the subject string with any matching patterns replaced.

Example

#ASSIGN($Text, 'This is a test.')

#/ Returns 'This is a monkey.'
#REGEXREPLACE(/test/, 'monkey', $Text)

#/ Returns 'This is a monkey.'
#REGEXREPLACE('/test/', 'monkey', $Text)

#/ Returns 'This is a test.'
#REGEXREPLACE(/TEST/, 'monkey', $Text)

#/ Returns 'This is a monkey.'
#REGEXREPLACE(/TEST/i, 'monkey', $Text)

#/ Returns 'This is a big test.'
#REGEXREPLACE(/is a (.*)$/, 'big $1', $Text)

Related

#REGEXMATCH, #REGEXSELECT