FLASH AS3 regular expression: RegExp

Source: Internet
Author: User
Tags character set regular expression

Regular expressions are one of the major features of as3.
1. Concepts of regular expressions


Regular expressions are the standard way to describe string features.

A regular expression consists of two parts: one part is the start position of a string indicated by a double slash, which is called the matching mode to describe the character string, and the other part is the letter behind the second slash, A flag called a regular expression is a combination of the five gimsx letters.

For example: var zfc: RegExp =/hello/g;


You can use the constructor of the regular expression class to generate a regular expression instance.

For example: var zfc: RegExp = new RegExp ("hello", "g ");


It can be seen that the constructor of a regular expression only splits the matching mode and flag into two strings and serves as a parameter to generate an instance of a regular expression. The flag is an optional parameter. If a letter is null or does not belong to "gimsx", the flag is ignored.

Use the constructor to generate regular expression instances. Note that because the matching mode is String type, note the escape characters, such as single quotation marks and double quotation marks.

2. Engine mechanism of regular expressions


AS3 uses a powerful NFA engine. The NFA engine is a Greedy execution method. Once a match is found, it returns immediately. No matter whether there are any matching characters in the end, the leftmost match is returned first.


3. Regular expression syntax


① Text symbols in regular expressions

A regular expression composed of text symbols is the simplest type. Text symbols include letters, symbols, and words. Chinese also belongs to text symbols, which are represented by Unicode codes.


There are three types of text symbols: common text and symbols, metacharacters, and invisible characters. However, if you use a regular expression constructor to match the forward slash, you do not need to escape it, but invisible characters still need to be escaped.

Character set in regular expressions

Character Set refers to the character set combination defined in brackets. As long as a character in the character set matches the feature description, it is considered a match. Character set can only match one character

In character sets, we can use the hyphen "-" to define a range. Three common character sets [A-Z], [a-z], [0-9]

The role of the inverse sign (^) in the character set is to take the inverse character set in the square brackets. That is, the anti-symbol is used. As long as it is a character that does not conform to the character set description, it will match.

Special characters to be escaped in the character set. We only need to escape "[^-" in the metacharacters of the regular expression character set.

③ Special period (.)

The regular expression uses "." to represent any character. But "." does not match the linefeed (n ). AS3 provides a single-line mode identifier (s) to match the "." character with the line break.

④ Selector (|)


"|" Indicates selection, which is used to match one of multiple possible regular expressions.

⑤ Brackets and grouping

Enclose a part of an expression in parentheses to define the expressions in groups. The group is operated as a whole. Such an operation is called a group.


Groups generally have the following applications:

● It is used to limit the range of the selector.

● You can specify the number of content repetitions in parentheses.

⑥ Use regular expressions to describe the repetition of strings

Explicit delimiters use braces {} and their numeric values to indicate the upper and lower limits of the number of occurrences of the pattern. In addition to explicit delimiters, there are also non-explicit delimiters.

If the delimiter is followed by the character set, the number of feature characters that match the character set description is limited.


If the delimiter is followed by a group, the number of feature strings that match the group expression is limited.

7. Regular expression operators and their meanings

"^" Matches the start position of the input string


"$" Matches the end position of the input string

"B" matches a word boundary, that is, the position between the word and space. KvvwH1D *'

"B" matches non-word boundaries


Five flag spaces of the regular expression


"G" global matches multiple

"I" ignoreCase is case insensitive


After "m" multiline sets this flag, $ and ^ can match the beginning and end of the line respectively.


After "s" dotall sets this flag, "." can match the line break (n)


"X" extended regular expressions that can be extended

The priority of the metacharacters in the sort regular expression.

% Z a-P
The same priority is always left to right, and the priority is higher first, then lower.


The metacharacter priority is as follows:


(),(? :),(? =), []

+ ,?., {N}, {n ,}, {n, m}


^, $, Character

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.