我是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";
答案 0 :(得分: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
这种方法可以避免浪费您和您计算机的资源