我想使用 VBScript 将字典类型数组传递给 Function。
我想创建一个可以在这样的数组上运行的正则表达式函数。
例如
array = [pat1:rep1, pat2:rep2]
'or any valid VBScript array or dictionary format
txtLn = regs()
Function regs(array)
Set regEx_ = new regExp
With regEx_
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = pat1:rep1
txtLn = regEx_.replace(txtLn, replace pattarn 1 from array)
.Pattern = pat1:rep2
txtLn = regEx_.replace(txtLn, replace pattarn 2 from array)
'and so on till the length of array
txtLn = regs
End function
我知道这是一个广泛的问题,但我没有任何想法。 我期待的是另一个想法。 请给我方向
最佳答案
看起来您确实想传递字典,因为您想传递映射到替换字符串的模式。尝试这样的事情:
Function regs(d)
Set regEx_ = New RegExp
With regEx_
.Global = True
.MultiLine = True
.IgnoreCase = True
For Each p In d.Keys
.Pattern = p
txtLn = regEx_.Replace(txtLn, d(p))
Next
End With
regs = txtLn
End Function
请注意,您需要将 txtLn
分配给函数名称以将结果传递回调用者。将 txtLn 作为参数传递到函数中而不是让函数对全局变量进行操作也是一个很好的做法。
关于arrays - 将模式和相应的替换字符串传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36764799/