Siebel eScript Language Reference > Siebel eScript Commands > String Objects >

string.replace() Method


This method searches a string using the regular expression pattern defined by pattern. If a match is found, it is replaced by the substring defined by replexp.

Syntax

string.replace(pattern, replexp)

Parameter
Description
pattern
Regular expression pattern to find or match in string.
replexp
Replacement expression which may be a string, a string with regular expression elements, or a function.

Returns

The original string with replacements according to pattern and replexp.

Usage

The string is searched using the regular expression pattern defined by pattern. If a match is found, it is replaced by the substring defined by replexp. The parameter replexp may be:

If any replacements are made, appropriate RegExp object static properties such as RegExp.leftContext, RegExp.rightContext, and RegExp.$n are set. These properties provide more information about the replacements.

The following table shows the special characters that may occur in a replacement expression.

Character
Description
$1, $2 ... $9
The text matched by regular expression patterns inside of parentheses. For example, $1 puts the text matched in the first parenthesized group in a regular expression pattern.
$+
The text matched by the last regular expression pattern inside of the last parentheses, that is, the last group.
$&
The text matched by a regular expression pattern.
$`
The text to the left of the text matched by a regular expression pattern.
$'
The text to the right of the text matched by a regular expression pattern.
\$
The dollar sign character.

Example

var rtn;
var str = "one two three two one";
var pat = /(two)/g;

// rtn == "one zzz three zzz one"
rtn = str.replace(pat, "zzz");

// rtn == "one twozzz three twozzz one";
rtn = str.replace(pat, "$1zzz");

// rtn == "one 5 three 5 one"
rtn = str.replace(pat, five());

// rtn == "one twotwo three twotwo one";
rtn = str.replace(pat, "$&$&);

function five() {
   return 5;

}

See Also

Typographic Conventions


 Siebel eScript Language Reference 
 Published: 18 April 2003