vbscript function clean string only allow certain characters
Until now I've been manually adding characters to replace that break my开发者_StackOverflow中文版 code. I'd like to be a bit more proactive, so I found this function that is supposed to replace everything EXCEPT valid characters. My first hurdle is that it doesn't seem to work. The code below is my full test file and the MsgBox comes up blank.
My second question is about performance. This function handles very, very large strings. Will this method be considerably slower? Anyone recommend anything else?
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
MsgBox (CleanUp("Test"))
Edit: I'm stupid and just saw the variable mixup I did which was causing it to return nothing. It is working now. Will still accept input for performance question or better suggestions.
You can simplify it even further.
objRegExp.Pattern = "[^\w+]"
It don't know what is the expected result for your example, but maybe you can try that for the pattern instead:
objRegExp.Pattern = "[^a-zA-Z0-9]"
Hope it works
精彩评论