0

I have the following string and want to remove the trailing comma. How can I accomplish this in VB script. I thought my replace should do it.

I have: str = [{"key" : "132904", }]

I want: [{"key" : "132904"}]

I'm doing: str = Replace(str, ", }]", "}]") but nothing is happening cause my string remains the same.

mo_maat
  • 1,672
  • 5
  • 30
  • 52

2 Answers2

0

The Replace statement is just fine for the mentioned purpose.

If I execute this code:

dim str
str = "[{""key"" : ""132904"", }]"
msgbox "original: " & vbNewLine & str
str = Replace(str, ", }]", "}]")
msgbox "After update: " & vbNewLine & str

I get

enter image description here

and then

enter image description here

The trailing "," is now gone.

Geert Bellekens
  • 10,760
  • 1
  • 16
  • 42
0

Replace statement and RegX.Replace works just fine for me.

dim str,RegX, SearchPattern, ReplacedText
Set RegX = NEW RegExp

str = "[{""key"" : ""132904"", }]"

' Use Regular expression
SearchPattern = ",.*}"
ReplaceString = "}"
RegX.Pattern = SearchPattern
RegX.Global = True
ReplacedText = RegX.Replace(str, ReplaceString)

msgbox "str: " & str & vbNewLine & _
       "Using Replace: " & Replace(str, ", }]", "}]")& vbNewLine & _
       "Using Regex: " & ReplacedText 

enter image description here

Om Choudhary
  • 405
  • 4
  • 17