Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The usage of RegExp object in VBS

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "the usage of RegExp object in VBS". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the usage of RegExp object in VBS".

The following code illustrates the use of the RegExp object:

Function RegExpTest (patrn, strng) Dim regEx, Match, Matches' create variables. Set regEx = New RegExp 'establishes the regular expression. RegEx.Pattern = patrn 'sets the mode. RegEx.IgnoreCase = True 'sets whether characters are case-sensitive. RegEx.Global = True 'sets global availability. Set Matches = regEx.Execute (strng) 'performs the search. For Each Match in Matches' traverses the matching set. RetStr = RetStr & "Match found at position" RetStr = RetStr & Match.FirstIndex & ".Match Value is'" RetStr = RetStr & Match.Value & "'." & vbCRLF Next RegExpTest = RetStrEnd FunctionMsgBox ("is.", "IS1 is2 IS3 is4")

The RegExp object, which has three properties and three methods, provides regular expression support in VBScript.

1) Execute method

This method is used to perform match detection on the specified regular expression, whose value returns a Matches collection containing all the Match objects that have detected matches. If no match is detected, an empty Matches collection is returned.

Syntax format: regexp.Execute (string)

Where regexp is the variable name of the RegExp object; string is a valid string expression for match detection.

2) Replace method

When calling the Replace method, if a character (string) that matches the specified regular expression is found in the specified string, it is replaced with the other character (string) specified. The return value of this method is the replaced string expression.

Syntax format: regexp.Replace (string1,string2)

Where regexp is the variable name of the RegExp object; string1 is the string expression to be detected and replaced; and string2 is the string expression for substitution.

Sub window_onLoad () dim str,regexpdim msgstrstr= "How are you" msgstr= before: "& str&"'/ / create RegExp object set regexp=new RegExp'// sets the regular expression regexp.Pattern= "o." / / sets whether to replace all matching regexp.Global= True [XSS _ clean] msgstr'// replacement operation msgstr=regexp.Replace (str, "test") msgstr= "after replacement: & msgstre [XSS _ clean] msgstrend sub

3) Test method

The purpose of this method is to determine whether there is anything in the specified string that matches the specified regular expression. If so, return Ture; otherwise return False. Similar to the Replace method, when the method is called, the regular expression is specified by the Pattern attribute. The difference is that the setting of the Global property has no effect on the method.

Sub window_onLoad () dim str,regexpdim blvarstr= "This is a test"'/ create RegExp object set regexp=new RegExp'// setting regular expression regexp.Pattern= ".s" / / call Test method blvar=regexp.Test (str) if blvar then [xss_clean] "found a match with" ®exp.pattern& "in" & str& "" else [xss_clean] "No match" end ifend sub Thank you for your reading The above is the content of "the usage of RegExp object in VBS". After the study of this article, I believe you have a deeper understanding of the usage of RegExp object in VBS, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report