被替换的文本的实际模式是通过 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 function
msgbox(replacetest("fox", "cat")) 将 fox 替换为 cat。
;另外,replace 方法在模式中替换 subexpressions 。 下面对以前示例中函数的调用,替换了原字符串中的所有字
对:
msgbox(replacetext("(\s+)(\s+)(\s+)", "$3$2$1")) 交换词对.
要求的脚本语言在5.0以上
