As3 Regular Expression

Source: Internet
Author: User

1. Create a regular expression in either of the following ways:
VaR exp1: Regexp = new Regexp ("ABCD", "G ");
VaR exp2 =/ABCD/g; // G Global indicates global match

Trace ("abcdefabcd". Match (exp1 ));
Trace ("abcdefabcd". Match (exp2); // output ABCD and ABCD, where the string. Match (expression) returns a matched array.

 

2. metacharacters
 
1) "." indicates matching any single character (such as: 1, E, medium, *, etc.). It can be a number, letter, Chinese character, punctuation, or other special characters.
For example:
Trace ("This is a good boo *". Match (/. OO./G); // return good, boo *
However, if the matched regular substring must contain ".", escape the substring, for example:
Trace ("This is a good boo.". Match (/. Oo \./G); // return boo.

2) "^" indicates the starting position of the matching string
(1) trace ("abaft \ nabalyn \ nabout ". replace (/^ AB/g, 'China'); // Replace "AB" at the start of the string with "China" to return

Back:
China aft \ nabalyn \ nabout
Enter the following information:
China aft
Abalyn
About

In multi-row matching, when the matching mode is specified as m, the matching starts after each \ n.

(2) trace ("abaft \ nabalyn \ nabout ". replace (/^ AB/GM, 'China'); // M multiline indicates that multiple rows match
Return Value:
China aft \ n China Alyn \ n China out
Enter the following information:
China aft
Alyn, China
China out


Note:

Trace ("abaft \ nabalyn \ nabout". Replace (/^ AB/, 'China '));

Trace ("abaft \ nabalyn \ nabout". Replace (/^ AB/m, 'China '));

The matching results of the above two methods are the same as those of (1). Therefore, to match multiple rows, "G" and "m" must be used at the same time.

3) $ indicates the end of the matching string. In a multi-line matching string, when the matching method is set to m, the matching starts before each \ n.

4) * indicates that the character or expression before it appears 0 or multiple times, that is, the meaning of the times, the number of times> = 0, if 0 or multiple times are satisfied, fetch appears multiple times
For example,/Bo */indicates that the O can appear 0 or multiple times. Bo, Boo, boook, B, and BK all match.

5) + indicates that the character or expression before it appears at least once, that is, the number of times> = 1. If one or more times meet the requirements, it will appear multiple times.
For example,/bo +/indicates that the O can appear more than once. Bo, Boo, and boook all match, but B and BK do not match.

6 )? It indicates that the character before the expression can appear once or not. If either or both of the two conditions are met, the expression appears once.
For example, trace ("This book is good boooooook boxbx". Replace (/bo? /G, '1 '));
Return Value: This one OK is good one ooooook one X
Here, the book satisfies both Bo and B, that is to say, O can also appear, take the first case, Bo

About minimum matching and maximum matching (Greedy mode and non-Greedy mode)

".*"

Matching "content a" "content B" "content c" only has one matching result, that is, "content a" "content B" "content C ",

Greedy mode matches as much content as possible, so starting from 1st double quotation marks until the last double quotation marks

".*? "

Match "content a" "content B" "content c"

The results have three matching results: "content a", "content B", and "content c". The non-Greedy mode matches as few as possible.

 

7) () indicates that a specific string of the regular expression is grouped into a group as a whole.
For example, trace ("Agogo is a name Ago's father". Replace (/A (GO) */g, 'har '));
Take go as a whole, that is, go can appear any time
Return Value: HA is Ha name Ha's father

Note: A group can be considered as a sub-pattern of the regular expression matching mode and can be treated as a common character.


(1)string.match()and regexp.exe C () capture the substring of the entire regular expression match, and capture the substring of the group match
For example:

Trace ("This is a good boboobooobooook". Replace (/(Bo +) */, 'book '));
Bo + indicates that it starts with B, where o appears more than once, and then appears any number of times in the form of a group.
Return Value: This is a good book
/(Bo +) */here is equivalent to/(Bo + )/

(2) reverse reference of a group, that is, reference the group defined above, such as/(Bo +). * \ 1kie/
Trace ("This book is a goodbookie". Replace (/(Bo +). * \ 1kie/, 'book '));
Return Value: This book
/(Bo +). * \ 1kie/here is equivalent to/(Bo +). * (Bo +) Kie/

(3 )? : The matched substring of the specified group is not captured.
Trace ("This book is a good bookie". Match (/B (O +) K ./));
Trace ("This book is a good bookie". Match (/B (? : O +) K ./));
Return Value:
Book, oo
Book

(4 )? = Forward lookup, that is, match first, and then obtain the matching results that meet a special condition.
For example:
Trace ('flashmx flash8 flashcs3 flash4 flash5'. Replace (/flash/g, '[flahsh]');
Trace ('flashmx flash8 flashcs3 flash4 flash5'. Replace (/flash (? = Cs)/g, '[flahsh]');

Return Value:
[Flahsh] MX [flahsh] 8 [flahsh] CS3 [flahsh] 4 [flahsh] 5
Flashmx flash8 [flahsh] CS3 flash4 flash5

/Flash (? = Cs)/g is added based on/flash/g (? = Cs ),
Three eligible items are flashcs3 flashcs4 flashcs5,


?! Search backward, that is, match first, and then obtain the matching results that meet certain special conditions.
For example:
Trace ('flashmx flash8 flashcs3 flashcs4 flashcs5'. Replace (/flash/g, '[flahsh]');
Trace ('flashmx flash8 flashcs3 flashcs4 flashcs5'. Replace (/flash (?! CS [3-5])/g, '[flahsh]');
Return Value:
[Flahsh] MX [flahsh] 8 [flahsh] CS3 [flahsh] cs4 [flahsh] cs5
[Flahsh] MX [flahsh] 8 flashcs3 [flahsh] cs4 [flahsh] cs5


/Flash (?! CS [3-5])/g is added based on/flash/g (?! CS [3-5]),
All items except flashcs3, flashcs4, and flashcs5

? = And ?! Deny


8) [] indicates a range. Only one character can be entered.
For example,/[BK]/B, K meets
/[A-zA-Z0-9] 26 lower-case letters, 26 upper-case letters, 10 digits all meet


(1)
Trace ("32o498dslkfjjlkjldsjfl70". Match (/[^ 0-9]/g ));
Trace ("32o498dslkfjjlkjldsjfl7 ^ 0". Match (/[^ 0-9]/g ));
Trace ("32o498dslkfjjlkjldsjfl7 ^ 0". Match (/[0-9 ^]/g ));

Return Value: O, D, S, L, K, F, J, J, L, K, J, L, D, S, J, F, L
O, D, S, L, K, F, J, J, L, K, J, L, D, S, J, F, L, ^
3, 2, 4, 9, 8, 7, ^, 0


If ^ is placed at the beginning of the [] character set, it indicates the inverse. For example, [^ 0-9] indicates a non-number, which is equivalent to \ D.
If the ^ character is placed at the beginning of the [] Character Set and the matched string contains the ^ character, it indicates the inverse meaning and uses the ^ As a general character

In this way, all non-numbers and ^ values are satisfied. The second trace () above ()

(2) In character sets, "." is not a metacharacter, but treated as a common character, which is equivalent to escaping "\."
Trace ('sdfdsfdfl34l3re. '. Match (/[0-9.]/g ));
Trace ('sdfdsfdfl34l3re. '. Match (/[0-9 \.]/g ));

Return Value:
3, 4, 3 ,.
3, 4, 3 ,.

(3) In character sets, only "-" and "\" are considered to be escaped,
For example, [0-9] indicates any number from 0 to 9.
[\ N \ t] indicates \ n line feed and \ t table feed.

Trace ('ldslfasdasd \ nksdjlsd \ t324123424 '. Replace (/[\ n \ t]/g, 'escape '));
Returns: ldslfasdasd escape ksdjlsd escape 324123424


9) | indicates either of them, similar [].
For example:/B | K/B, K meets
/Gook | book/gook, book all meet
Trace ("This is a good cook book". Replace (/Good | book/g, 'har '));
Return Value: This is a ha cook

Note: (1)
Trace ("This is a good book". Replace (/[GK]/g, 'har '));
Trace ("This is a good book". Replace (/g | K/g, 'har '));

Return Value: This is a ha Ood boo ha
This is a ha Ood boo ha
The above two expressions have the same effect.

However: trace ("This is a good cook book". Replace (/Good | book/g, 'har '));
Trace ("This is a good cook book". Replace (/[goodbook]/g, 'har '));

Return Value: This is a ha cook
This is a hahahahaha C hahahahahaha
The effects of the above two expressions are different, because | the good and book are regarded as a whole, and [] the good and book
Together, that is, goodbook, that is, as long as any character in G, O, D, B, and K is satisfied.


10)-represents a range, which is used []
For example: trace ("lLJ62-76DS5LFd-4Jlk-China". Match (/[A-Z \-]/g ));
[A-Z \-] Where "-" in A-Z is a metacharacter that represents a range from A to Z, and "-" after \-escape

 


3. Meta Sequence
1) {n} indicates that the character or expression before it must be repeated n times, that is, the number of times = N
For example:/bo {2}/, where o must be repeated twice, Boo is satisfied, and boook is not satisfied

2) {n,} indicates that the character or expression before it must be repeated more than N times, that is, the number of times> = n. If both N and n + 1 are met, n + 1, that is, the largest
For example:/bo {2,}/, where o must be repeated more than 2 times, boo must meet, boook must also meet

3) {n, m} indicates that the character or expression before it must be repeated n to m times or more, that is, n <= Times <= M. If n is satisfied, n + 1, n + 1, that is, the largest
For example:
Trace ("This book is good boooooook boxbx". Replace (/bo {2, 6}/g, 'har '));
Return Value: This ha K is good Ha OK boxbx

Trace ("This book is good boook boxbx". Replace (/bo {2, 6}/g, 'har '));
Return Value: This Haha K is good Haha K boxbx

4) \ D indicates matching numeric characters, that is, 0-9
\ D indicates matching non-numeric characters with \ D, that is, characters other than 0-9
\ W indicates matching a word character, that is, 26 lower-case letters, 26 upper-case letters, 10 digits, and underscores.
\ W indicates that non-word characters are matched, which is not negative to \ W.
\ S indicates matching any blank characters
\ S indicates matching any non-spatial character, which is not negative to \ s.

 


4. Mark
I ignorecase case-insensitive
G Global global match
M multiline multi-line mode
S dotall specifies whether the metacharacter "." matches "\ n", that is, "\ n" is also processed as a common character.
For example:
Trace ("this is a book \ nkie". Replace (/bo + K. Kie/g, 'har '));
Trace ("this is a book \ nkie ". replace (/bo + K. kie/GS, 'ha'); // here, \ n is considered as a common character

Return Value:
This is a book
Kie
This is a ha

X extended mode. In extended mode, the blank characters in the regular expression are ignored. For example, the effect of/C _ d/is equivalent to/C_D/

 


5. Attributes of Regular Expressions
Source can get the matching mode of the regular expression, such as trace (/\ D/. source); output: \ D
Lastindex indicates the starting position of the Regular Expression in the string. It takes effect only when it is marked as G.

 

6. Regular Expression Method
Exec (string) returns an object
Test (string) returns a Boolean value.

Supplement:

The as3 regular expression can only match the content of the same row. If the matched content is scattered to multiple rows, it cannot match;

Therefore, if necessary, replace the line break (content = content. Replace (/\ n/GI ,"");)

Then perform other matches.

 

From: http://hi.baidu.com/wojiubaibudu/item/20259da5991a8cdd5bf1910f

As3 Regular Expression

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.