一种(有点复杂)的方法:
Option Explicit
Dim g_reItem ' sorry, must be global
Class cReMagic
Private m_reMenu
Private Sub Class_Initialize()
Set m_reMenu = New RegExp
m_reMenu.Global = True
m_reMenu.IgnoreCase = True
m_reMenu.MultiLine = True
m_reMenu.Pattern = "^\[menu\]([\s\S]+?)^\[/menu\]$"
Set g_reItem = New RegExp
g_reItem.Global = True
g_reItem.IgnoreCase = True
g_reItem.MultiLine = True
' item 1|http://stackoverflow.com/1
' <a href="http://stackoverflow.com/1">Item 1</a>
g_reItem.Pattern = "^\s*([^|]+)\|(http:.+?)(\r)$"
End Sub
Public Function Replace(sInp)
Replace = m_reMenu.Replace(sInp, GetRef("MenuRpl"))
End Function
End Class
Function MenuRpl(sM, sG1, nPos, sSrc)
MenuRpl = "<ul>" & g_reItem.Replace(sG1, GetRef("ItemRpl")) & "</ul>"
End Function
Function ItemRpl(sM, sG1, sG2, sG3, nPos, sSrc)
ItemRpl = vbCrLf & "<a href=""" & sG2 & """>" & sG1 & "</a>"
End Function
Dim sInp : sInp = Join(Array( _
"Lorem ipsum dolor sit amet, consecte" _
, "" _
, "[menu]" _
, "item 1|http://stackoverflow.com/1" _
, "item 2|http://stackoverflow.com/2" _
, "item 3|http://stackoverflow.com/3" _
, "[/menu]" _
, "" _
, "[mune]" _
, "item 1|http://stackoverflow.com/1" _
, "item 2|http://stackoverflow.com/2" _
, "item 3|http://stackoverflow.com/3" _
, "[/mune]" _
, "[menu]" _
, "item 4|http://stackoverflow.com/4" _
, "[/menu]" _
, "" _
, "Lorem ipsum dolor sit amet, consecte" _
), vbCrLf)
WScript.Echo sInp
WScript.Echo "------------------"
Dim oReMagic : Set oReMagic = New cReMagic
WScript.Echo oReMagic.Replace(sInp)
WScript.Echo "------------------"
输出:
Lorem ipsum dolor sit amet, consecte
[menu]
item 1|http://stackoverflow.com/1
item 2|http://stackoverflow.com/2
item 3|http://stackoverflow.com/3
[/menu]
[mune]
item 1|http://stackoverflow.com/1
item 2|http://stackoverflow.com/2
item 3|http://stackoverflow.com/3
[/mune]
[menu]
item 4|http://stackoverflow.com/4
[/menu]
Lorem ipsum dolor sit amet, consecte
------------------
Lorem ipsum dolor sit amet, consecte
<ul>
<a href="http://stackoverflow.com/1">item 1</a>
<a href="http://stackoverflow.com/2">item 2</a>
<a href="http://stackoverflow.com/3">item 3</a>
</ul>
[mune]
item 1|http://stackoverflow.com/1
item 2|http://stackoverflow.com/2
item 3|http://stackoverflow.com/3
[/mune]
<ul>
<a href="http://stackoverflow.com/4">item 4</a>
</ul>
Lorem ipsum dolor sit amet, consecte
------------------