user2269104
user2269104

Reputation: 205

Extract JS array string in VBS

I'm coding in VBSscript.

I have the following string

myString = "[ "http://www.google.com/", "http://www.yahoo.com" ]

How can I extract the urls with regexp? is there anyway to read this JS array?

Upvotes: 0

Views: 103

Answers (2)

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38765

RegExp for easy cases (no embeded "):

  ' Mark the correct quotes
  Dim sTest : sTest = "[ ""http://www.google.com/"", ""http://www.yahoo.com"", """" ]"
  WScript.Echo sTest
  Dim reStringLiteral : Set reStringLiteral = New RegExp
  reStringLiteral.Global  = True
  reStringLiteral.Pattern = """([^""]*)"""
  Dim oMTS : Set oMTS = reStringLiteral.Execute(sTest)
  Dim oMT
  For Each oMT In oMTS
      WScript.Echo oMT.Value, oMT.SubMatches(0)
  Next

output:

[ "http://www.google.com/", "http://www.yahoo.com", "" ]
"http://www.google.com/" http://www.google.com/
"http://www.yahoo.com" http://www.yahoo.com
""

Split() for easy cases (invariant ", " separator):

  Dim sTest : sTest = "[ ""http://www.google.com/"", ""http://www.yahoo.com"", """" ]"
  WScript.Echo sTest
  Dim reNetto : Set reNetto = New RegExp
  reNetto.Global  = True
  reNetto.Pattern = "^[^""]*""|""[^""]*$"
  sTest = reNetto.Replace(sTest, "")
  WScript.Echo sTest
  Dim sUrl
  For Each sUrl In Split(sTest, """, """)
      WScript.Echo qq(sUrl), sUrl
  Next

output:

[ "http://www.google.com/", "http://www.yahoo.com", "" ]
http://www.google.com/", "http://www.yahoo.com", "
"http://www.google.com/" http://www.google.com/
"http://www.yahoo.com" http://www.yahoo.com
""

Upvotes: 0

Laurent S.
Laurent S.

Reputation: 6947

I would remove starting and trailing brackets, then use the split method. I don't think regex are intended to do that...

Upvotes: 2

Related Questions