8 out of 18 rated this helpful - Rate this topic

Replace Method (VBScript)

Updated: March 2009

Replaces text found in a regular expression search.

object.Replace(string1, string2) 
object

Required. Always the name of a RegExp object.

string1

Required. String1 is the text string in which the text replacement is to occur.

string2

Required. String2 is the replacement text string.

The actual pattern for the text being replaced is set using the Pattern property of the RegExp object.

The Replace method returns a copy of string1 with the text of RegExp.Pattern replaced with string2. If no match is found, a copy of string1 is returned unchanged.

The following code illustrates use of the Replace method.

Function ReplaceTest(patrn, replStr)
  Dim regEx, str1

  str1 = "The quick brown fox jumps over the lazy dog."

  ' Create regular expression.
  Set regEx = New RegExp
  regEx.Pattern = patrn
  regEx.IgnoreCase = True

  ' Make replacement.
  ReplaceTest = regEx.Replace(str1, replStr)
End Function

MsgBox(ReplaceTest("fox", "cat"))      ' Replace 'fox' with 'cat'.

In addition, the Replace method can replace subexpressions in the pattern. The following call to the function shown in the previous example swaps the first pair of words in the original string:

' Swap first pair of words.
MsgBox(ReplaceTest("(\S+)(\s+)(\S+)", "$3$2$1"))

Date

History

Reason

March 2009

Reformatted code in examples.

Information enhancement.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Using a matchevaluator in a Visual Basic 6.0 / VBA application
The replace method has an alternate syntax.

object.Replace(string1, matchevaluator)

matchevaluator

    Required. Matchevaluator is a custom function template that returns a replacement text string.

VBScript example

See "What's New in Windows Script 5.5", http://msdn.microsoft.com/en-us/library/ms974619.aspx

Visual Basic 6.0/VBA example

Sub SimpleTest()
    Dim matchevaluator As Class1
    Dim re As RegExp
    Dim s As String
   
    s = "Hello there"
    Set matchevaluator = New Class1
    Set re = New RegExp
    re.Pattern = "\w+"
    re.Global = True
    MsgBox re.Replace(s, matchevaluator)
End Sub

Put the matchevaluator inside a class module.

Public Function Test(ParamArray a()) As String
    n = UBound(a)
   
    If n > 1 Then
        Test = "matched '" & a(0) & "' at position " & a(n - 1) & "..."
    End If
End Function

Make the matchevaluator the default member.

To make the matchevaluator the default member in a VBA application, you'll need to export out the class module, edit it, and import it back in.

VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "Class1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Public Function Test(ParamArray a()) As String
Attribute Value.VB_UserMemId = 0
    n = UBound(a)
   
    If n > 1 Then
        Test = "matched '" & a(0) & "' at position " & a(n - 1) & "..."
    End If
End Function
VBScript Function to remove or replace specified characters in a string
The article linked below explains how to remove or replace specific characters from a string. The code sample is provided contains a VBScript function to parse a string, replacing or removing any character found in the array declared at the beginning of the function.

VBScript String Clean Function - Remove/Replace Illegal Charaters

The function was originally writted to remove illegal characters found in a string for use as a document file name in SharePoint Document Library.

To use the function, simply update the array at the beginnning of the function to include all of the characters which are to be removed or replaced. If replacing specific characters with something else, the you will need to set inside the finction the string which will replace each character if found.

The VBScript function then returns a cleaned string.

VBScript String Clean Function - Remove/Replace Illegal Charaters