I have a vbscript that inserts some strings into a database. Often, these strings have weird characters, quotes, apostrophes, ampersands, etc. I am currently using the following string to replace all but certain characters, but it’s replacing semicolons and some other stuff (including spaces) that I would ideally like to keep. So I’m basically looking for the least restrictive regex that will still generate a sql safe string.
For what it’s worth, the strings are Windows installed applications (as you would see in Add/Remove Programs).
Function CleanUp (input)
Dim objRegExp, outputStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = "((?![a-zA-Z0-9]).)+"
outputStr = objRegExp.Replace(input, "-")
objRegExp.Pattern = "\-+"
outputStr = objRegExp.Replace(outputStr, "-")
CleanUp = outputStr
End Function
This seems to work great so far in my testing:
I didn’t know you could specify a range of ascii values. This covers everything between Space and ~, excluding a single quote (‘).