且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

替换 vbscript 中的特殊字符

更新时间:2023-09-17 23:37:16

您可以使用正则表达式添加您认为是非特殊字符的每个字符.

stringsToCheck = Array("Windows Live Fot¢t r", _"Galer¡a fotogr fica de Windows Live", _Windows Live Maker")设置正则表达式 = 新正则表达式regExp.IgnoreCase = TrueregExp.Global = 真regExp.Pattern = "[^a-z0-9 !?@]" '在此处添加您不认为是特殊字符的每个字符对于stringsToCheck中的每个strstrProcessed = regExp.Replace(str, "?")WScript.Echo "旧字符串:" &字符串WScript.Echo "新字符串:" &字符串处理下一个

输出:

旧字符串:Windows Live Fot¢t r新字符串:Windows Live Fot?t rOld String: Galer¡a fotogr fica de Windows Live新字符串:Galer?a fotogr fica de Windows Live旧字符串:Windows Live Maker新字符串:Windows Live Maker

I have a set of strings that may or may not have special characters in it. Example:

Windows Live Fot¢t r
Galer¡a fotogr fica de Windows Live
Windows Live Maker

What i wanna do is to:

  1. check whether the whole string contains a special character in it
  2. If yes, replace these characters with a "?"

I haven't tried anything yet since i'm a newbie in vb scripting.

You can use a regular expression where you add every character that you consider as a non-special character.

stringsToCheck = Array("Windows Live Fot¢t r", _
                       "Galer¡a fotogr fica de Windows Live", _
                       "Windows Live Maker")

Set regExp = New RegExp
regExp.IgnoreCase = True
regExp.Global = True
regExp.Pattern = "[^a-z0-9 !?@]" 'Add here every character you don't consider as special character

For each str In stringsToCheck
    strProcessed = regExp.Replace(str, "?")
    WScript.Echo "Old String: " & str
    WScript.Echo "New String: " &  strProcessed
Next

Output:

Old String: Windows Live Fot¢t r
New String: Windows Live Fot?t r
Old String: Galer¡a fotogr fica de Windows Live
New String: Galer?a fotogr fica de Windows Live
Old String: Windows Live Maker
New String: Windows Live Maker