当前位置: 首页 > 图文教程 > 脚本技术 > VBScript > VBS教程:方法-Replace 方法

VBScript
VBS教程:VBscript语句-Execute 语句
VBS教程:VBscript语句-Erase 语句
VBS教程:VBscript语句-Do...Loop 语句
VBS教程:VBscript语句-Dim 语句
VBS教程:VBscript语句-Const 语句
VBS教程:VBscript语句-Class 语句
VBS教程:VBscript语句-Call 语句
VBS教程:VBscript语句-功能介绍
VBS教程:VBscript属性-Value 属性
VBS教程:VBscript属性-Source 属性
VBS教程:VBscript属性-Pattern 属性
VBS教程:VBscript属性-Number 属性
VBS教程:VBscript属性-Length 属性
VBS教程:VBscript属性-IgnoreCase 属性
VBS教程:VBscript属性-HelpFile 属性
VBS教程:VBscript属性-HelpContext 属性
VBS教程:VBscript属性-Global 属性
VBS教程:VBscript属性-FirstIndex 属性
VBS教程:VBscript属性-Description 属性
VBS教程:VBscript属性-功能介绍

VBScript 中的 VBS教程:方法-Replace 方法


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-11   浏览: 36 ::
收藏到网摘: n/a

Replace 方法

替换在正则表达式查找中找到的文本。

object.Replace(string1, string2)

参数

object

必选项。总是一个 RegExp 对象的名称。

string1

必选项。string1 是将要进行文本替换的字符串。

string2

必选项。 string2 是替换文本字符串。

说明

被替换的文本的实际模式是通过 RegExp 对象的 Pattern 属性设置的。

Replace 方法返回 string1 的副本,其中的 RegExp.Pattern 文本已经被替换为 string2。如果没有找到匹配的文本,将返回原来的 string1 的副本。

下面的例子说明了 Replace 方法的用法。

Function ReplaceTest(patrn, replStr) Dim regEx, str1 ' 建立变量。 str1 = "The quick brown fox jumped over the lazy dog." Set regEx = New RegExp ' 建立正则表达式。 regEx.Pattern = patrn ' 设置模式。 regEx.IgnoreCase = True ' 设置是否区分大小写。 ReplaceTest = regEx.Replace(str1, replStr) ' 作替换。End FunctionMsgBox(ReplaceTest("fox", "cat")) '  'fox' 替换为 'cat'

;另外,Replace 方法在模式中替换 subexpressions 。 下面对以前示例中函数的调用,替换了原字符串中的所有字对:

MsgBox(ReplaceText("(\S+)(\s+)(\S+)", "$3$2$1")) ' 交换词对.