All along, I used the New RegExp to create regular expression objects:
Set regex = new RegExp However, it is also possible to create a regular expression object that is new to the VBS:
Set regex = CreateObject ("VBScript.RegExp") it seems that none of the VBScript books I've read has been written about this, and of course there's still a lot of stuff that's not on the books.
Now that a regular expression is mentioned, by the way, the regular expression object of VBS supports MultiLine mode, and the object has a MultiLine property. You can see the properties and methods of the COM object by running Visual C + + with its own OLE View, the left tree expanding Type libraries, and finding Microsoft VBScript Regular Expressions 5.5:
Copy Code code as follows:
Interface Iregexp2:idispatch {
[ID (0x00002711), propget]
HRESULT pattern ([out, retval] bstr* ppattern);
[ID (0x00002711), propput]
HRESULT pattern ([in] BSTR Ppattern);
[ID (0x00002712), propget]
HRESULT IgnoreCase ([out, retval] variant_bool* pignorecase);
[ID (0x00002712), propput]
HRESULT IgnoreCase ([in] Variant_bool pignorecase);
[ID (0x00002713), propget]
HRESULT Global ([out, retval] variant_bool* pglobal);
[ID (0x00002713), propput]
HRESULT Global ([in] Variant_bool Pglobal);
[ID (0x00002717), propget]
HRESULT Multiline ([out, retval] variant_bool* pmultiline);
[ID (0x00002717), propput]
HRESULT Multiline ([in] Variant_bool pmultiline);
[ID (0x00002714)]
HRESULT Execute (
[In] BSTR sourcestring,
[Out, retval] idispatch** ppmatches);
[ID (0x00002715)]
HRESULT Test (
[In] BSTR sourcestring,
[Out, retval] variant_bool* Pmatch);
[ID (0x00002716)]
HRESULT Replace (
[In] BSTR sourcestring,
[In] VARIANT Replacevar,
[Out, retval] bstr* pdeststring);
};
However, many books are not introduced, Vbsedit automatic completion of the function does not hint MultiLine attributes, even Microsoft's official reference manual is not.
Finally, for a simple example, if you want to delete a blank line in the text, you can use the following code:
Copy Code code as follows:
' Author:demon
' Link:http://demon.tw/programming/vbs-regexp-object.html
' Date:2011/12/26
Dim FSO, regex, str
Set fso = CreateObject ("Scripting.FileSystemObject")
Set regex = CreateObject ("VBScript.RegExp")
str = FSO. OpenTextFile ("Demon.txt"). ReadAll
Regex. Global = True
Regex. MultiLine = True
Regex. Pattern = "^\s*\n"
str = regex. Replace (str, "")
' The equivalent of str in javascript = str.replace (/^\s*\n/gm, "")
Fso. OpenTextFile ("Demon.txt", 2). Write Str
Original from: http://demon.tw/programming/vbs-regexp-object.html