DIM re2:Set re2 = New RegExp
WITH re2
.Pattern = "dol amp"
.IgnoreCase = True
.Global = True
MsgBox .Replace("put dol amp here","ok$&done")
' Expected: put ok$&done here
' Observed: put okdol ampdone here
END WITH
预期基于文档:
“正则表达式(RegExp)对象”
https://msdn.microsoft.com/en-us/library/yab2dx62(v=vs.84).aspx
https://msdn.microsoft.com/en-us/library/k9z80300(v=vs.84).aspx
“正则表达式简介(脚本)”
https://msdn.microsoft.com/en-us/library/6wzad2b2(v=vs.84).aspx
对比成功:
DIM re2:Set re2 = New RegExp
WITH re2
.Pattern = "dol amp"
.IgnoreCase = True
.Global = True
MsgBox .Replace("put dol amp here","ok$ &done")
' Expected: put ok$ &done here
' Observed: as expected
END WITH
答案 0 :(得分:2)
未记载的内容与undefined相同。虽然VBScript文档在某些方面缺乏(特别是在涉及正则表达式的更精细细节时),但这种行为并非完全出乎意料。微软有很长的历史,通过加倍来转义具有特殊含义的字符(VBScript中最突出的例子是字符串文字中的双引号)。并且&
是"整个匹配"的共同元字符。在许多正则表达式实现中。
如果您按下文档,可以查看documentation for the .Net regular expression engine。它的功能集基本上是VBScript引擎中实现的功能的超集。
总结:
$&
是对整个匹配的引用。$1
,$2
等等backreferences用于捕获群组。$&
,$1
,$2
等,请将$
翻倍。答案 1 :(得分:-1)
因为在替换替换字符串中,$&是一个具有特殊意义的保留序列,尽管文档没有提到这一点。
解决方法:提供您很高兴回复未记录的行为,您可以逃避$& e.g。
DIM re2:Set re2 = New RegExp
WITH re2
.Pattern = "dol amp"
.IgnoreCase = True
.Global = True
MsgBox .Replace("put dol amp here",Replace("ok$&done","$","$$"))
' Expected: put ok$&done here
' Observed: as expected
END WITH
观察到的:
读者不满意未定义的行为,请参阅此处:https://stackoverflow.com/questions/40426225/re-the-issue-of-literal-in-regexp-replace-pattern-does-anyone-have-a-solutio