To read the article online, visit

VBScript Search and Replace Code

This article was written by Toby Gramm, webmaster of In the article is a function which will search through a text string, replacing one substring with another.

SearchAndReplace(expression, find, replacewith)

Ok, sure you could use the VBScript Replace() Function, but try moving your script into VB.
Darn!, No replace funciton in VB. So I use this, and it works great in Access too!
Note: VB6 has the replace() function

Dim s
s = "Oh no, this string has stuff I don't want, like this"
Response.Write SeachAndReplace(s, "I don't want, ","")

Oh no, this string has stuff like this

Da' Code

'Written by: Toby Gramm
Function SearchAndReplace(s, Str1, Str2)
	Dim pos
	s = s & "" 'Fix Null
	pos = InStr(s, Str1)
	While pos > 0
		s = Mid(s, 1, pos - 1) & Str2 & Mid(s, pos + Len(Str1))
	    pos = InStr(pos, s, Str1)
	SearchAndReplace = s
End Function

[Visit]   [Email Toby Gramm]

Article Information
Article Title: VBScript Search and Replace Code
Article Author: Scott Mitchell
Published Date: Tuesday, September 29, 1998
Article URL:

Copyright 2018 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers