-1

我是 vbs 新手,有一个问题。我有一个 js 变量文件,上面写着 var varname = "variable"; 在每一行。我想在 vbs 的引号内创建一个数组。我不知道如何使这项工作。

' Sample vbs
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("path", ForReading)

Const ForReading = 1

Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

'Then you can iterate it like this
For Each strLine in arrFileLines
leftquote = InStr(strLine,"""")
rightquote = InStrRev(strLine,"""")
str=CStr(strLine)
leng = rightquote-leftquote
WScript.Echo Mid(str,leftquote+1,leng)


Next

这是js

// Sample js
var a = new Array();
a[0] = "1";
a[1] = "2";
a[2] = "3";
a[3] = "4";
4

1 回答 1

0

使用 RegExp 提取整个文件内容的双引号之间的数据。在代码中:

  Dim goFS : Set goFS = CreateObject( "Scripting.FileSystemObject" )
  Dim reCut : Set reCut = New RegExp
  reCut.Global = True
  reCut.Pattern = """([^""]+)"""
  Dim sAll : sAll = goFS.OpenTextFile("..\data\sample.js").ReadAll()
  WScript.Echo sAll
  Dim oMTS : Set oMTS = reCut.Execute(sAll)
  If 0 = oMTS.Count Then
     WScript.Echo "parse error"
  Else
     ReDim aAll(oMTS.Count - 1)
     Dim i
     For i = 0 To UBound(aAll)
         aAll(i) = oMTS(i).SubMatches(0) ' or: aAll(i) = CStr(oMTS(i).SubMatches(0))
     Next
     WScript.Echo Join(aAll, ", ")
  End If

输出:

// Sample js
var a = new Array();
a[0] = "1";
a[1] = "2";
a[2] = "3";
a[3] = "4";

1, 2, 3, 4

这种方法可以避免浪费您和您的计算机资源

  1. 创建一个可有可无的行数组(处理第 i 行时不需要访问第 j 行)
  2. 滚动你自己的“解析器”
于 2012-11-19T08:17:31.280 回答