VBS tutorial: introduction to regular expressions – using regular expressions


Using regular expressions

In a typical search and replace operation, you must provide the exact text to find. This technology may be sufficient for simple search and replacement tasks in static text, but due to its lack of flexibility, it is difficult or even impossible to search dynamic text.

Using regular expressions, you can:

  • A pattern of the test string. For example, you can test an input string to see if there is a phone number mode or a credit card number mode in the string. This is called data validation.
  • Replace text. You can use a regular expression in a document to identify specific text, and then you can delete it all or replace it with another text.
  • Extract a substring from a string based on pattern matching. Can be used to find specific text in text or input fields.

For example, if you need to search the entire web site to remove some outdated materials and replace some HTML formatting tags, you can use regular expressions to test each file to see if there is the material or HTML formatting tag you want to find in the file. In this way, the affected files can be narrowed down to those containing the materials to be deleted or changed. Regular expressions can then be used to remove outdated materials, and finally, regular expressions can be used again to find and replace those tags that need to be replaced.

Another example of how useful regular expressions are is a language whose string processing power is not yet known. VBScript is a subset of visual basic and has rich string processing functions. Visual Basic Scripting Edition similar to C does not have this capability. Regular expressions have significantly improved the string processing power of Visual Basic Scripting Edition. However, it may be more efficient to use regular expressions in VBScript, which allows multiple string operations to be performed in a single expression.