替换 vbscript 中的特殊字符 [英] Replace special characters in vbscript

查看:92
本文介绍了替换 vbscript 中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组字符串,其中可能包含也可能不包含特殊字符.示例:

Windows Live Fot¢tGaler¡a fotogr fica de Windows LiveWindows Live Maker

我想做的是:

<块引用>

  1. 检查整个字符串中是否包含特殊字符
  2. 如果是,请用?"替换这些字符

我还没有尝试过任何东西,因为我是 vb 脚本的新手.

解决方案

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

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

这篇关于替换 vbscript 中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆