VBScript to Find and replace Regular expression with a regular expression in a text file

I am building a jenkins jobs for build comparison. I want to replace double newline (nn) in the text file with single new line (n). After that I want to replace every instance of word “commit” with a newline and commit i.e “ncommit”. I want to use a vbscript for this, anyone can suggest how to do this?

Currently I am using following vbscript:

Const ForReading = 1
Const ForWriting = 2

strName = Wscript.Arguments(0)
strOriginal = Wscript.Arguments(1)
strReplacement = Wscript.Arguments(2)

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strName, ForReading)

strText = objFile.ReadAll

' Replace desired string
Set objRegExp = New RegExp
objRegExp.Global = True
objRegExp.IgnoreCase = False
objRegExp.Pattern = strOriginal
strReplacement = objRegExp.Replace(strText, strReplacement)

Set objFile = objFSO.OpenTextFile(strName, ForWriting)
objFile.Write strReplacement

The script replaces regex patterns with a normal string, i.e it replaces double newline with a normal string i.e with string n. I don’t know how to extend this to replace regex with regex as I am new to VBScript.

Source: regex

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.