Normal characters consist of all those printed and non-printed characters that are not explicitly specified as metacharacters. This includes all uppercase and lowercase alphabet characters, all numbers, all punctuation marks, and some symbols.
The simplest regular expression is a single normal character that matches the character itself in the searched string. For example, the single-character pattern 'A' can match the letter 'A' that appears anywhere in the searched string. Here are some examples of single-character regular expression patterns:
/a/ /7/ /M/The equivalent VBScript single-character regular expression is:
a 7 MMultiple single characters can be combined together to get a larger expression. For example, the following JScript regular expression is nothing else, but an expression created by combining single-character expressions 'a', '7' and 'M'.
/a7M/The equivalent VBScript expression is:
a7MPlease note that there is no connection operator here. All you need to do is put one character behind another character.
There are many metacharacters that need special processing when trying to match them. To match these special characters, you must first escape them, that is, use a backslash (/) in front of it. The following table gives these special characters and their meanings:
| Special characters | illustrate |
|---|---|
| $ | Matches the end position of the input string. If the Multiline property of the RegExp object is set, $ also matches '/n' or '/r'. To match the $ character itself, use /$. |
| ( ) | Marks the start and end positions of a subexpression. Subexpressions can be obtained for later use. To match these characters, use /( and /). |
| * | Matches the previous subexpression zero or multiple times. To match the * character, use /*. |
| + | Matches the previous subexpression once or more times. To match the + character, use /+. |
| . | Match any single character except line break/n. To match., use /. |
| [ | Mark the beginning of a bracket expression. To match [, use /[. |
| ? | Matches the previous subexpression zero or once, or indicates a non-greedy qualifier. To match the ? character, use /?. |
| / | Mark the next character as a special character, or a primitive character, or a backward reference, or an octal escape character. For example, 'n' matches the character 'n'. '/n' matches the newline character. The sequence '//' matches /, and '/(' matches (. |
| ^ | Matches the start position of the input string unless used in a square bracket expression, at which point it means that the character collection is not accepted. To match the ^ character itself, use /^. |
| { | Tag the beginning of the qualifier expression. To match {, use /{. |
| | | Specify a choice between two items. To match |, use /|. |
There are many useful non-print characters that must be used occasionally. The following table shows the escape sequences used to represent these non-printed characters:
| character | meaning |
|---|---|
| /c x | Matches the control characters specified by x . For example, /cM matches a Control-M or carriage return. The value of x must be one of AZ or az. Otherwise, treat c as an original 'c' character. |
| /f | Match a page break. Equivalent to /x0c and /cL. |
| /n | Match a newline character. Equivalent to /x0a and /cJ. |
| /r | Match a carriage return character. Equivalent to /x0d and /cM. |
| /s | Match any whitespace characters, including spaces, tabs, page breaks, etc. Equivalent to [/f/n/r/t/v]. |
| /S | Match any non-whitespace characters. Equivalent to [^/f/n/r/t/v]. |
| /t | Match a tab character. Equivalent to /x09 and /cI. |
| /v | Match a vertical tab. Equivalent to /x0b and /cK. |
Period (.) matches any single printed or non-printed character in a string, except for line breaks (/n). The following JScript regular expressions can match 'aac', 'abc', 'acc', 'adc', etc., and can also match 'a1c', 'a2c', ac' and a#c':
/ac/The equivalent VBScript regular expression is:
acIf you try to match a string containing the file name, where the period (.) is part of the input string, you can prefix a period in the regular expression with a backslash (/) character to achieve this. For example, the following JScript regular expression can match 'filename.ext':
/filename/.ext/For VBScript, the equivalent expression looks like this:
filename/.extThese expressions are still quite limited. They are only allowed to match any single character. In many cases, it is very useful for matching special characters from a list. For example, if the input text contains chapter titles denoted by numbers as Chapter 1, Chapter 2 and so on, you might want to find these chapter titles.
You can put one or more single characters in a square bracket ([ and ]) to create a list to match. If the characters are enclosed in brackets, the list is called a bracket expression . In brackets, like anywhere else, ordinary characters represent themselves, that is, they match one of themselves that appears in the input text. Most special characters lose their meaning when they are in bracket expressions. Here are some exceptions:
The characters contained in the bracket expression match only one single character where the bracket expression is located in the regular expression. The following JScript regular expressions can match 'Chapter 1', 'Chapter 2', 'Chapter 3', 'Chapter 4', and 'Chapter 5':
/Chapter [12345]/To match the same chapter title in VBScript, use the following expression:
Chapter [12345]Please note that the positional relationship between the word 'Chapter' and the following spaces and the characters in brackets is fixed. Therefore, the bracket expression is only used to specify a set of characters that satisfy the single character position immediately after the word 'Chapter' and a space. Here is the ninth character position.
If you want to use a range instead of the character itself to represent the character to be matched, you can use a hyphen to separate the start and end characters of the range. The character value of each character will determine its relative order within a range. The following JScript regular expression contains a range expression equivalent to the parentheses list shown above.
/Chapter [1-5]/The expressions of the same function in VBScipt are as follows:
Chapter [1-5]If a range is specified in this way, both the start and the end values are included in that range. One thing that needs to be noted is that the starting value in Unicode sort must be before the end value.
If you want to include hyphens in a bracket expression, you must use one of the following methods:
[/-] [-az] [az-] [!--] [!-~]Similarly, by placing a caret (^) at the beginning of the list, you can find all characters that are not in the list or range. If the caret appears elsewhere in the list, it matches itself without any special meaning. The following JScript regular expression matches the title of the chapter with a chapter number greater than 5:
/Chapter [^12345]/For VBScript, use:
Chapter [^12345]In the example shown above, the expression will match any numeric character at the ninth position except 1, 2, 3, 4, or 5. So, 'Chapter 7' is a match, and so is 'Chapter 9'.
The above expression can be represented by a hyphen (-). For JScript:
/Chapter [^1-5]/Or, for VBScript, it is:
Chapter [^1-5]A typical use of bracket expressions is to specify a match to any uppercase or lowercase alphabetical characters or any number. The following JScript expression gives this match:
/[A-Za-z0-9]/The equivalent VBScript expression is:
[A-Za-z0-9]