日期:2014-05-16  浏览次数:20358 次

vbs 如何引用正则匹配的内容
如:Pattern= " <div   id=cnt> ([.+]) </div> "

如何写vbs正则表达式实现引用([.+])的内容

------解决方案--------------------
<script language=vbs>
dim str
str= " <div id=cnt> aaa </div> "
set re = new RegExp
re.Pattern = " <\/?div\s*[^> ]*> "
re.Global = true
re.test(str)
msgbox re.replace(str, " ")
</script>
------解决方案--------------------
要捕获是吧?
------解决方案--------------------
Visual Basic Scripting Edition

SubMatches 集合
请参阅
For Each...Next 语句 | Match 对象 | Matches 集合 |RegExp 对象
要求
版本 5.5
正则表达式子匹配字符串的集合。

说明
SubMatches 集合包含了单个的子匹配字符串,只能用 RegExp 对象的 Execute 方法创建。SubMatches 集合的属性是只读的。

运行一个正则表达式时,当圆括号中捕捉到子表达式时可以有零个或多个子匹配。SubMatches 集合中的每一项是由正则表达式找到并捕获的的字符串。

下面的代码演示了如何从一个正则表达式获得一个 SubMatches 集合以及如何它的专有成员:

Function SubMatchTest(inpStr)
Dim oRe, oMatch, oMatches
Set oRe = New RegExp
' 查找一个电子邮件地址(不是一个理想的 RegExp)
oRe.Pattern = "(\w+)@(\w+)\.(\w+) "
' 得到 Matches 集合
Set oMatches = oRe.Execute(inpStr)
' 得到 Matches 集合中的第一项
Set oMatch = oMatches(0)
' 创建结果字符串。
' Match 对象是完整匹配 — dragon@xyzzy.com
retStr = "电子邮件地址是: " & oMatch & vbNewline
' 得到地址的子匹配部分。
retStr = retStr & "电子邮件别名是: " & oMatch.SubMatches(0) ' dragon
retStr = retStr & vbNewline
retStr = retStr & "组织是: " & oMatch. SubMatches(1) ' xyzzy
SubMatchTest = retStr
End Function

MsgBox(SubMatchTest( "请写信到 dragon@xyzzy.com 。谢谢! "))