JScript and VBScript Regular Expressions 1th-2 page _ Regular expressions

Source: Internet
Author: User
Tags alphabetic character control characters lowercase printable characters

Regular expressions
If you have not used regular expressions before, you may not be familiar with this term or concept. However, they are not as novel as you might think.

Recall how you found the file on your hard disk. Are you sure you will use it? and * characters to help find the file you are looking for. The character matches a single character in the file name, while the * matches one or more characters. One like ' data?. DAT ' mode can be found in the following files:

Data1.dat
Data2.dat
Datax.dat
DataN.dat
If you use the * character instead? Character, the number of files found will be enlarged. ' Data*.dat ' can match all of the following file names:

Data.dat
Data1.dat
Data2.dat
Data12.dat
Datax.dat
DataXYZ.dat
Although this method of searching for files is certainly useful, it is very limited. and * The limited ability of wildcards allows you to have a concept of what regular expressions can do, but regular expressions are more powerful and flexible.

Early origins
The "ancestors" of regular expressions can be traced back to early studies of how the human nervous system works. Warren McCulloch and Walter Pitts, two neuroscientists, have developed a mathematical way of describing these neural networks.

In 1956, a mathematician named Stephen Kleene, based on the early work of McCulloch and Pitts, published a paper entitled "Representation of neural network events", introducing the concept of regular expressions. A regular expression is an expression that describes what he calls the algebra of a regular set, so the term "regular expression" is used.

Subsequently, it was found that this work could be applied to some early studies using Ken Thompson's computational Search algorithm, and Ken Thompson was the main inventor of Unix. The first practical application of regular expressions is the QED editor in Unix.

As they say, the rest is a well-known history. From then until now regular expressions are an important part of text-based editors and search tools.

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

Using regular expressions, you can:

Tests a pattern of a string. For example, you can test an input string to see if there is a phone number pattern or a credit card number pattern in the string. This is known as data validation.
Replaces text. You can use a regular expression in your document to identify specific text, and then you can delete it all, or replace it with another text.
Extracts 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 material and replace some HTML formatting tags, you can use regular expressions to test each file to see if there are any material or HTML formatting tags that you want to find in the file. With this method, you can narrow the affected file range to those files that contain the material you want to delete or change. You can then use regular expressions to delete obsolete materials, and finally, you can use regular expressions again to find and replace those that need to be replaced.

Another example that illustrates the usefulness of regular expressions is a language whose string-handling power is not yet known. VBScript is a subset of Visual Basic that has rich string processing capabilities. Jscript similar to C does not have this ability. Regular expressions make a noticeable improvement in the string handling capabilities of JScript. However, it may be more efficient to use regular expressions in VBScript, which allows multiple string operations to be performed in a single expression.



A regular expression is a literal pattern consisting of ordinary characters (such as characters A through Z) and special characters (called metacharacters). This pattern describes one or more strings to be matched when looking for a text body. A regular expression is used as a template to match a character pattern with the string being searched for.

Here are some examples of regular expressions that you might encounter:

JScript VBScript Match
/^\[\t]*$/"^\[\t]*$" matches a blank line.
/\d{2}-\d{5}/"\d{2}-\d{5}" verifies that an ID number consists of a 2-digit number, a hyphen, and a 5-digit number.
/< (. *) >.*<\/\1>/"< (. *) >.*<\/\1>" matches an HTML tag.


The following table is a complete list of metacharacters and its behavior in the context of regular expressions:

Character description
\ marks the next character as a special character, or a literal character, or a backward reference, or a octal escape character. For example, ' n ' matches the character ' n '. ' \ n ' matches a newline character. Sequence ' \ ' matches ' \ ' and ' \ (' Matches ' (".

^ matches the start position of the input string. If the Multiline property of the RegExp object is set, ^ also matches the position after ' \ n ' or ' \ R '.
$ matches the end position of the input string. If the Multiline property of the RegExp object is set, the $ also matches the position before ' \ n ' or ' \ R '.

* Match the preceding subexpression 0 or more times. For example, zo* can match "z" and "Zoo". * is equivalent to {0,}.

+ matches the preceding subexpression one or more times. For example, ' zo+ ' can match "Zo" and "Zoo", but cannot match "Z". + is equivalent to {1,}.

? Match the preceding subexpression 0 times or once. For example, "Do (es)" can match "do" in "do" or "does". is equivalent to {0,1}.

{n} n is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match ' o ' in ' Bob ', but can match two o in ' food '.

{N,} n is a non-negative integer. Match at least n times. For example, ' o{2,} ' cannot match ' o ' in ' Bob ' but can match all o in ' Foooood '. ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.

{n,m} m and n are non-negative integers, where n <= m. Matches n times at least and matches up to M times. For example, "o{1,3}" will match the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' o '. Notice that there is no space between the comma and the two number.

? When the character is immediately following any of the other qualifiers (*, +,?, {n}, {n,}, {n,m}), the matching pattern is not greedy. Non-greedy patterns match as few strings as possible, while the default greedy pattern matches as many of the searched strings as possible. For example, for the string "oooo", ' o+? ' will match a single "O", and ' o+ ' will match all ' o '.

. Matches any single character except "\ n". To match any character including ' \ n ', use a pattern like ' [. \ n] '.
(pattern) matches the pattern and gets the match. The obtained match can be obtained from the generated matches collection,

The Submatches collection is used in VBScript and the $0...$9 property is used in JScript. To match the parentheses character, use ' \ (' or ' \ ').

(?:p Attern) matches pattern but does not get matching results, which means that this is a non fetch match and is not stored for later use. This is useful for combining parts of a pattern with the "or" character (|). For example, ' Industr (?: y|ies) is a more abbreviated expression than ' industry|industries '.

(? =pattern) forward lookup, matching the find string at the beginning of any string matching pattern. This is a non-fetch match, that is, the match does not need to be acquired for later use. For example, ' Windows (? =95|98| nt|2000) ' Can match windows in Windows 2000, but cannot match windows in Windows 3.1. It does not consume characters, that is, after a match occurs, the next matching search begins immediately after the last match, instead of starting after the character that contains the pre-check.

(?! pattern), which matches the lookup string at the beginning of any string that does not match the pattern. This is a non-fetch match, that is, the match does not need to be acquired for later use. For example, ' Windows (?! 95|98| nt|2000) ' Can match windows in Windows 3.1, but cannot match windows in Windows 2000. It does not consume characters, that is, after a match occurs, the next matching search begins immediately after the last match, instead of starting after the character that contains the pre-check.

X|y matches x or Y. For example, ' Z|food ' can match "z" or "food". ' (z|f) Ood ' matches ' zood ' or ' food '.

[XYZ] Character set combination. Matches any one of the characters contained. For example, ' [ABC] ' can match ' a ' in ' plain '.

[^XYZ] Negative character set combination. Matches any characters that are not included. For example, ' [^ABC] ' can match ' P ' in ' plain '.

[A-z] character range. Matches any character within the specified range. For example, ' [A-z] ' can match any lowercase alphabetic character in the range ' a ' to ' Z '.

[^a-z] a negative character range. Matches any character that is not in the specified range. For example, ' [^a-z] ' can match any character that is not in the range of ' a ' to ' Z '.

\b Matches a word boundary, which refers to the position between the word and the space. For example, ' er\b ' can match ' er ' in ' never ', but cannot match ' er ' in ' verb '.

\b Matches a non word boundary. ' er\b ' can match ' er ' in ' verb ', but cannot match ' er ' in ' Never '.

\CX matches the control characters indicated by X. For example, \cm matches a control-m or carriage return character. The value of x must be a-Z or
One-A-Z. Otherwise, c is treated as a literal ' C ' character.

\d matches a numeric character. equivalent to [0-9].


\d matches a non-numeric character. equivalent to [^0-9].

\f matches a page feed character. Equivalent to \x0c and \CL.

\ n matches a newline character. Equivalent to \x0a and \CJ.

\ r matches a carriage return character. Equivalent to \x0d and \cm.

\s matches any white space character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].

\s matches any non-white-space character. equivalent to [^ \f\n\r\t\v].

\ t matches a tab character. Equivalent to \x09 and \ci.

\v matches a vertical tab. Equivalent to \x0b and \ck.

\w matches any word character that includes an underscore. Equivalent to ' [a-za-z0-9_] '.

\w matches any non word character. Equivalent to ' [^a-za-z0-9_] '.

\XN matches N, where n is the hexadecimal escape value. The hexadecimal escape value must be a determined two digits long. For example, ' \x41 ' matches ' A '. ' \x041 ' is equivalent to ' \x04 ' & ' 1 '. You can use ASCII encoding in regular expressions ...

\num matches num, where num is a positive integer. A reference to the match that was obtained. For example, ' (.) \1 ' matches two consecutive identical characters.

\ n identifies a octal escape value or a backward reference. n is a backward reference if you have at least n obtained subexpression before \ nthe. Otherwise, if n is an octal number (0-7), then N is an octal escape value.

\NM identifies a octal escape value or a backward reference. NM is a backward reference if at least NM has obtained the subexpression before \nm. If there are at least N fetches before \nm, then n is a backward reference followed by a literal m. If all the preceding conditions are not satisfied, if both N and M are octal digits (0-7), then \nm will match octal escape value nm.

\NML if n is an octal number (0-3) and both M and L are octal digits (0-7), then the octal escape value NML is matched.

\un matches N, where N is a Unicode character represented in four hexadecimal digits. For example, \u00a9 matches the copyright symbol (?).


Establish regular expressions

The method for constructing regular expressions is the same as for creating mathematical expressions. That is, using multiple metacharacters and operators to combine small expressions to create larger expressions.

You can construct a regular expression by putting together various components of an expression pattern between a pair of delimiters. For JScript, the delimiter is a pair of forward slash (/) characters. For example:

/expression/
For VBScript, a pair of quotation marks ("") is used to determine the bounds of the regular expression. For example:

"Expression"
In the two examples shown above, regular expression patterns (expression) are stored in the Pattern property of the RegExp object.

The component of a regular expression can be a single character, character set, character range, selection between characters, or any combination of any of these components.

Order of Precedence

After a regular expression is constructed, it can be evaluated like a mathematical expression, that is, it can be evaluated from left to right and in order of precedence.

The following table lists the order of precedence for the various regular expression operators, from highest priority to lowest priority:

Operator description
\ escape Character
(), (?:), (? =), [] parentheses and square brackets
*, +,?, {n}, {n,}, {n,m} qualifier
^, $, \anymetacharacter position and order
| "or" action


Ordinary characters

Normal characters are made up of all print and nonprinting characters that are not explicitly specified as metacharacters. This includes all uppercase and lowercase alphabetic characters, all numbers, all punctuation marks, and some symbols.

The simplest regular expression is a single ordinary 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 word single-character patterns:

/a/
/7/
/m/
The equivalent VBScript word single-character expression is:

A
"7"
M
You can combine multiple single characters together to get a larger expression. For example, the following JScript regular expression is nothing more than an expression created by combining a single character expression ' a ', ' 7 ', and ' M '.

/a7m/
The equivalent VBScript expression is:

"A7m"
Please note that there are no connection operators here. All you have to do is put one character behind another character.

Special characters


There are a number of metacharacters that require special processing when trying to match them. To match these special characters, you must first escape the characters, which means that you use a backslash (\) earlier. These special characters and their meanings are given in the following table:

Special Character description
$ matches the end position of the input string. If the Multiline property of the RegExp object is set, then $ also matches ' \ n ' or ' \ R '. To match the $ character itself, use \$.

() marks the start and end position of a subexpression. The subexpression can be obtained for later use. To match these characters, use \ (and \).

* Match the preceding subexpression 0 or more times. To match the * character, use \*.

+ matches the preceding subexpression one or more times. to match the + character, use \+.

. Matches any single character except the newline character \ n. to match., please use \.

[Marks the beginning of a bracket expression. To match [, use \[.

? Matches the preceding subexpression 0 or more times, or indicates a non-greedy qualifier. Want to match? characters, please use \?.

\ marks the next character as either a special character, or a literal character, or a backward reference, or a octal escape character. For example, ' n ' matches the character ' n '. ' \ n ' matches line breaks. The sequence ' \ \ ' matches ' \ ' and ' \ (' matches '.

^ matches the starting position of the input string, unless used in a bracket expression, at which point it means that the character set is not accepted. to match

^ character itself, please use \^.

{marks the beginning of a qualifier expression.} To match {, use \{.

| Indicates a choice between two items. to match |, use \|.


Non-printable characters

There are a number of very useful nonprinting characters that must be used occasionally. The following table shows the escape sequences used to represent these nonprinting characters:

Character meaning
\CX matches the control characters indicated by X. For example, \cm matches a control-m or carriage return character. The value of x must be one-a-Z or a-Z. Otherwise, c is treated as a literal ' C ' character.

\f matches a page feed character. Equivalent to \x0c and \CL.

\ n matches a newline character. Equivalent to \x0a and \CJ.

\ r matches a carriage return character. Equivalent to \x0d and \cm.

\s matches any white space character, including spaces, tabs, page breaks, and so on. equivalent to [\f\n\r\t\v].

\s matches any non-white-space character. equivalent to [^ \f\n\r\t\v].

\ t matches a tab character. Equivalent to \x09 and \ci.

\v matches a vertical tab. Equivalent to \x0b and \ck.

Character matching
a period (.) matches any single printed or nonprinting character in a string, except for line breaks (\ n). The following JScript regular expressions can match ' AAC ', ' abc ', ' ACC ', ' ADC ' and so on, and can also match ' A1c ', ' a2c ', a-c ' and a#c ':

/a.c/
The equivalent VBScript regular expression is:
Current 1/2 page 12 Next read the full text

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.