VBScript Replace Function

Definition and Usage

The Replace function can replace one string with another specified number of times.

Syntax

Replace(string,find,replacewith[,start[,count[,compare]]])
parameter Description
string Required. The string that needs to be searched.
find Required. The part of the string that will be replaced.
replacewith Required. The substring to be replaced.
start Optional. Specifies the starting position. The default is 1.
count Optional. Specifies the number of replacements to be made. The default is -1, indicating all possible replacements.
compare Optional. Specifies the type of string comparison to be used. The default is 0.

Value of parameter compare:

Constant Value Description
vbBinaryCompare 0 Perform binary comparison.
vbTextCompare 1 Perform text comparison.

Possible values returned by Replace:

Possible parameter values Value returned by Replace
expression is zero-length Zero-length string ("").
expression is Null Error.
find parameter is zero-length Copy of expression.
replacewith parameter is zero-length Copy of expression, with all content specified by the find parameter removed.
start > Len(expression) Zero-length string.
count is 0 Copy of expression.

Instance

Example 1

dim txt
txt="This is a beautiful day!"
document.write(Replace(txt,"beautiful","terrible"))

Output:

This is a terrible day!