arrays - 将模式和相应的替换字符串传递给函数

标签 arrays vba vbscript

我想使用 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/

相关文章:

java - ArrayList java用作数组

c++ - 匹配2个数组中的数字c++

mysql - 如何在 Excel 中自动执行 SQL 脚本

vba - 使用范围作为自动筛选方法的标准数组

json - 解析 JSON、MS Access VBA(嵌套循环)

.net - vbscript 检查是否安装了.net 2.0

php - 如何在Mysql中检查数组中的列

java - 使用其中的乘法从另一个数组填充一个数组

batch-file - 批处理或 vb 脚本从 Excel 文件读取 URL 并将 URL 附加文件下载到指定位置或目录

vbscript - 在 vbscript 中将变量参数传递给 ShellExecute