Javascript advanced training-Regular Expressions

Source: Internet
Author: User
Tags first string

Three test questions:
1, var str = "abbbbacc ";
VaR rs = Str. Replace ("A", "0 ");
(A) 0 bbbbacc (B) 0bbbb0cc
2, var str = "abbbbacc ";
VaR rs = Str. Replace ("/A/", "0 ");
(A) 0 bbbbacc (B) 0bbbb0cc
3, var str = "abbbbacc ";
VaR rs = Str. Replace ("/A/G", "0 ");
(A) 0 bbbbacc (B) 0bbbb0cc
Note: The replace method signature of the string object is:
Replace (regx, STR)

I. Overview

1. Regular Expressions can be said to be a mechanism provided by any programming language. It mainly provides the ability to process strings.
2. Use Cases of Regular Expressions in page processing:
1) form verification. Verify that some domains comply with certain rules. For example, the input box must be an email or a contact number.
2) process the DOM model. For example, if an expression is used to locate an object or a series of objects in the Dom, an example is to locate the DIV object containing a special character in the ID attribute.
3) Pure programming logic. Directly used in programming logic.
3. Note: The code snippets of the regular expression mentioned in this section are all tested, but note that the definition of the character string for line breaks, we use a format similar to the following:
VaR STR = "It's is
A beautiful city ";
It is wrong to directly write this form in JS Code. How can we get a string with a line break? A simple method: Enter the text in textarea and wrap the text, and then assign the value to the JS variable. For example:
VaR STR = Document. Forms [0]. mytextarea. value;

Ii. syntax and usage

1. Define a regular expression

1) Regular Expressions can be defined in two forms: Normal Mode and constructor mode.
2) Common method: var Reg =/expression/additional parameter
Expression: a string that represents a rule. Some special characters can be used to represent special rules.
Additional parameters: used to extend the meaning of the expression. Currently, there are three main parameters:
G: indicates that global matching can be performed.
I: It indicates case-insensitive matching.
M: indicates that multiple rows can be matched.
The preceding three parameters can be combined at any time to indicate a combination of meanings. Of course, they can also be left blank.
Example:
VaR Reg =/a * B /;
VaR Reg =/ABC + F/g;
3) constructor: var Reg = new Regexp ("expression", "additional parameter ");
The meanings of "expressions" and "Additional Parameters" are the same as those defined above.
Example:
VaR Reg = new Regexp ("A * B ");
VaR Reg = new Regexp ("ABC + F", "G ");
4) differences between common methods and constructor Methods
In normal mode, the expression must be a constant string, and the expression in the constructor can be a constant string or a JS variable, for example, using the user input as an expression parameter:
VaR Reg = new Regexp (document. Forms [0]. exprfiled. Value, "g ");

2. expression mode

1) expression mode refers to the expression and style, that is, how to describe the "expression" in the VaR Reg =/expression/additional parameter?
2) In terms of standardization, expression modes are classified into simple and compound modes.
3) simple mode: it refers to the mode expressed by the combination of common characters, such
VaR Reg =/abc0d /;
It can be seen that the simple mode can only represent a specific match.
4) compound mode: it refers to a mode that contains wildcards for expression, for example:
VaR Reg =/A + B? /W /;
Among them, + ,? And/W both belong to wildcards, which indicate special meanings. Therefore, the composite mode can express more abstract logic.
The following describes the meanings and usage of each wildcard in the composite mode.
5) special characters in composite mode:

1>/: Used as escape characters in many programming languages. Generally
If the symbol is followed by a common character C,/C represents a special meaning. For example, N originally represents the character N, but/n represents a line break.
/If the symbol is followed by a special character C, then/C represents a common character C. For example,/is generally used as an escape character, but // indicates a common character /.
The usage of/in the Regular Expression in Javascript is the same as that in the above, but it is only different from that in programming languages. The special orders table may not be the same.

2> ^: match the start end of the input string. If multiple rows match, that is, if the additional parameter of the expression contains m, it will also match after a line break.
Example:/^ B/matches the first B in "Bab BC"
Example 2:/^ B/GM match
"BADD B
CDAF
B dsfb"
The first B in the first row and the first B in the third row

3> $: match the end of the input character. If multiple rows match, that is, if the additional parameter of the expression contains m, it is also matched before a line break.
It is the opposite of ^.
Example:/T $/matches t in "Bat" but does not match t in "hate"
Example 2:/T $/match
"Tag
Bat"
The last T in the first row and the T in the second row.

4> *: match the first character 0 or multiple times.
Example:/AB */matches "abbbb" in "dddabbbbc" and "A" in "ddda"

5> +: match the first character once or multiple times.
Example:/AB +/matches "abbbb" in "dddabbbbc", but does not match "ddda"
Similar to the subsequent {1,} (prototype: {n ,})

6>? :? In general, it is used to match the previous character 0 times or once, but it has two other special usage:
If it is followed by *, + ,? And {}, it indicates the minimum matching times of the original match, for example:
/Ba */matches "Baaaa" in "bbbaaaa", but/Ba *? /Matches "B" in "bbbaaaa" (because * Indicates 0 or multiple matches? It should indicate the minimum number of matches, that is, 0 matches ).
Likewise:/BA ++? /Matches "ba" in "Baaaa ".
As a syntax structure symbol, it is used in predicate, that is, the X (? = Y) and X (?! = Y)

7>.: The "." In the decimal point matches any single character, except for line breaks.
What are the total characters in the standard? See Character Set
For example,/a. B/matches "ACB" in "acbaa", but does not match "abbb ".

8> (x): Indicates matching X (not a specific character X or a specific character, X indicates a string), and matching will be remembered. In the syntax, this () it is called "capturing parentheses", which is the parentheses used for capturing.
Matching is remembered because some functions in the function provided by the expression return an array, which stores all matched strings, such as the exec () function.
In addition, the premise that X in () is remembered is to match X.
Example 1:
VaR regx =/A (B) C /;
VaR rsw.regx.exe C ("abcddd ");
As can be seen from the above,/A (B) C/matches "ABC" in "abcddd". Because of (), B will also record it, so the number returned by RS is:
{ABC, B}
Example 2:
VaR regx =/A (B) C /;
VaR rsw.regx.exe C ("acbcddd ");
RS returns NULL. Because/A (B) C/does not match "acbcddd", B in () is not recorded (although the string contains B)
 
9> (? : X): matches X, but does not remember X. () in this format is called "non-capturing parentheses", that is, parentheses for non-capturing.
Example:
VaR regx =/(? : B) C /;
VaR rsw.regx.exe C ("abcddd ");
From the above we can see that/(? : B) C/matches "ABC" in "abcddd" because (? :), B won't record it, so the number returned by RS is:
{ABC}

10> X (? = Y): Match X, only when followed by Y. If the match is met, only X is remembered, and Y is not remembered.
Example:
VaR regx =/user (? = Name )/;
VaR rsw.regx.exe C ("the username is Mary ");
Result: The matching is successful, and the RS value is {user}

11> X (?! Y): matches X, only when y is not followed. If the match is met, only X is remembered, and Y is not remembered.
Example:
VaR regx =/user (?! Name )/;
VaR rsw.regx.exe C ("the user name is Mary ");
Result: The matching is successful, and the RS value is {user}
Example 2:
VaR regx = // D + (?! /.)/;
VaR rsw.regx.exe C ("54.235 ");
Result: matching result. The RS value is {5}. 54 is not matched because 54 is followed by ". ", of course, 235 also matches, but because of the action of the exec method, 235 will not be returned

12> X | Y: matches X or Y. Note that if both X and Y match, remember only X.
Example:
VaR regx =/Beijing | Shanghai /;
VaR rsw.regx.exe C ("I love Beijing and Shanghai ");
Result: The matching is successful. The RS value is {Beijing}. Although Shanghai matches, it is not remembered.

13> {n}: match the first character n times.
N must be a non-negative number. If n is a negative number or decimal number, no syntax error is reported.
Example:
VaR regx =/AB {2} C /;
VaR rsw.regx.exe C ("abbcd ");
Result: The matching is successful. The RS value is {abbc }.

14> {n ,}: match the first character at least N times.
Example:
VaR regx =/AB {2,} C /;
VaR rsw.regx.exe C ("abbcdabbbc ");
Result: The matching is successful. The RS value is {abbc }. Note that the reason why abbbc meets the criteria is not remembered. This is related to the behavior of the exec method, which will be explained later.

15> {n, m}: match the first character at least N times at most m times.
As long as N and m are numbers and M> = N, NO syntax error is reported.
Example:
VaR regx =/AB {2, 5} C /;
VaR rsw.regx.exe C ("abbbcd ");
Result: The matching is successful. The RS value is {abbbc }.
Example 2:
VaR regx =/AB {2, 2} C /;
VaR rsw.regx.exe C ("abbcd ");
Result: The matching is successful. The RS value is {abbc }.
Example 3:
VaR regx =/AB (2, 5 )/;
VaR rsw.regx.exe C ("abbbbbbbbbbbb ");
Result: The matching is successful. The RS value is {abbbbb}. This indicates that if the first character appears more than m times, only m times are matched. In addition:
VaR regx =/AB (2, 5) C /;
VaR rsw.regx.exe C ("abbbbbbbbbbbbc ");
Result: The matching fails. The RS value is null. Why does the matching fail? Because B (2, 5) matches the first five B ,, in expression/AB () C/, B is followed by C, but B is followed by 5 B in the string, so an error is reported.

16> [xyz]: XYZ represents a string. This mode matches a character in [], and the form of [xyz] is equivalent to [X-Z].
Example:
VaR regx =/A [BC] D /;
VaR rsw.regx.exe C ("abddgg ");
Result: The matching is successful. The RS value is {abd}
Example 2:
VaR regx =/A [BC] D /;
VaR rsw.regx.exe C ("ABCD ");
Result: The matching fails. The RS value is null. [BC] indicates that the RS matches one of B or C but does not match the same time.

17> [^ XYZ]: This mode matches a character other than []. The form of [^ XYZ] is equivalent to [^ X-Z].
Example:
VaR regx =/A [^ BC] D /;
VaR rsw.regx.exe C ("afddgg ");
Result: The matching is successful. The RS value is {AFD}
Example 2:
VaR regx =/A [^ BC] D /;
VaR rsw.regx.exe C ("Abd ");
Result: The matching fails. The RS value is :.

18> [/B]: match the return key.

19>/B: match the boundary of a word, such as space and line break. Of course, when matching a line break, the expression should be appended with the parameter M.
Example:
VaR regx = // BC ./;
VaR rsw.regx.exe C ("Beijing is a beautiful city ");
Result: The matching is successful. The RS value is {CI}. Note that the leading space of C does not match the result, that is, {CI} is incorrect.

20>/B: represents a non-word boundary.
Example:
VaR regx = // bi ./;
VaR rsw.regx.exe C ("Beijing is a beautiful city ");
Result: The matching is successful. The RS value is {IJ}, that is, it matches IJ in Beijing.

21>/CX, matching a control character. For example,/cm matches a control-M or
Carriage return. The value of X must be either a A-Z or a-Z. Otherwise, consider C as
The original 'C' characters. (The actual example must be supplemented)

21>/D: match a number character, equivalent to [0-9].
Example:
VaR regx =/user/D /;
VaR rsw.regx.exe C ("user1 ");
Result: The matching is successful. The RS value is {user1}

22>/D: match a non-numeric character, equivalent to [^ 0-9].
Example:
VaR regx =/user/D /;
VaR rsw.regx.exe C ("usera ");
Result: The matching is successful. The RS value is {usera}

23>/F: match a page break.

24>/N: match a line break. Because it is a line break, the M parameter must be added to the expression.
Example:
VaR regx =/A/NBC/m;
VaR STR ="
BC ";
VaR rsw.regx.exe C (STR );
Result: The matching is successful. The RS value is: {}. If the expression is/A/n/rb/, It is not matched, therefore, in the General Editor, a "enter" key represents "Carriage Return", rather than "line feed", at least in the textarea domain.
25>/R: match a carriage return.

26>/S: match a space character, equivalent to [/f/n/R/T/V/u00a0/u2028/u2029].
Example:
VaR regx = // Si /;
VaR rsw.regx.exe C ("Beijing is a city ");
Result: The matching is successful. The RS value is {I}

27>/S: match a non-space character, equivalent to [^/f/n/R/T/V/u00a0/u2028/u2029].
Example:
VaR regx = // Si /;
VaR rsw.regx.exe C ("Beijing is a city ");
Result: The matching is successful. The RS value is {Ei}

28>/T: match a tab
Example:
VaR regx =/A/TB /;
VaR rsw.regx.exe C ("a bc ");
Result: The matching is successful. The RS value is {a bc}

29>/V: match a vertical Tab

30>/W: match a number, _, or alphabet character, that is, [A-Za-z0-9 _].
Example:
VaR regx = // w /;
VaR rsw.regx.exe C ("$25.23 ");
Result: The matching is successful. The RS value is {2}

31>/W: match a non-digit, _, or alphabet character, that is, [^ A-Za-z0-9 _].
Example:
VaR regx = // w /;
VaR rsw.regx.exe C ("$25.23 ");
Result: The matching is successful. The RS value is {$}

32>/N: note that N is not/n. Here N is a positive integer that matches the characters in the N.
Example:
VaR regx =/user ([,-]) group/1 Role /;
VaR rsw.regx.exe C ("user-group-role ");
Result: The matching is successful. The RS value is {user-group-role,-}. The matching for user, group, and role is also successful. However, for example, user-group, role and so on are incorrect.

33>/0: match a NUL character.

34>/xhh: match a character expressed by two hexadecimal numbers.

35>/uhhhh: match a character expressed by a four-digit hexadecimal number.

3. Expression operation

1) expression operation. Here it refers to the expression-related methods. We will introduce six methods.
2) expression object (Regexp) method:

1> exec (STR), returns the first string in STR that matches the expression and is expressed as an array. Of course, if the expression contains parentheses for capturing, the returned array may also contain matching strings in (), for example:
VaR regx = // D + /;
VaR rs1_regx.exe C ("3432ddf53 ");
The returned RS value is {3432}
VaR regx2 = new Regexp ("AB (/d +) C ");
VaR rs2jwregx2.exe C ("ab234c44 ");
The returned RS value is {ab234c, 234}
In addition, if there are multiple suitable matches, the first matching is returned for the first execution of exec. If the first matching is continued, the second and third matching are returned in sequence. For example:
VaR regx =/user/D/g;
VaR rs1_regx.exe C ("ddduser1dsfuser2dd ");
VaR rs1=regx.exe C ("ddduser1dsfuser2dd ");
Then the RS value is {user1} and the RS value is {rs2}. Of course, note that the G parameter in regx is required. Otherwise, the first match is returned no matter how many times exec is executed. There are also relevant content to explain this imagination.

2> test (STR): checks whether the string 'str' matches the expression and returns a Boolean value. For example:
VaR regx =/user/d +/g;
VaR flag = regx. Test ("user12dd ");
The flag value is true.

3) String object Method

1> match (expr): returns an array of strings that match expr. If parameter G is not added, the first match is returned. If parameter G is added, all matches are returned.
Example:
VaR regx =/user/D/g;
VaR STR = "user13userddduser345 ";
VaR rs = Str. Match (regx );
RS value: {user1, user3}

2> Search (expr) returns the first matched index value in the string that matches expr.
Example:
VaR regx =/user/D/g;
VaR STR = "user13userddduser345 ";
VaR rs = Str. Search (regx );
The RS value is 0.

3> Replace (expr, STR), replace the part matching expr in the string with Str. In addition, in the replace method, STR can contain a variable symbol $, in the format of $ N, representing the matching string of the nth to be remembered in the match (note that parentheses can be used for memory matching ).
Example:
VaR regx =/user/D/g;
VaR STR = "user13userddduser345 ";
VaR rs = Str. Replace (regx, "00 ");
RS value: 003userddd0045
Example 2:
VaR regx =/U (SE) R/D/g;
VaR STR = "user13userddduser345 ";
VaR rs = Str. Replace (regx, "$1 ");
RS value: se3userdddse45
Pay special attention to the Replace (expr, STR) method. If expr is an expression object, it will be replaced globally (in this case, the expression must be appended with the parameter G, otherwise, only the first match is replaced. If expr is a string object, only the first matched part is replaced. For example:
VaR regx = "user"
VaR STR = "user13userddduser345 ";
VaR rs = Str. Replace (regx, "00 ");
RS value: 0013userddduser345

4> split (expr): splits the string by matching the expr part, returns an array, and the result is the same if the expression is attached with the parameter G.
Example:
VaR regx =/user/D/g;
VaR STR = "user13userddduser345 ";
VaR rs = Str. Split (regx );
RS value: {3 userddd, 45}

4. expression-related attributes

1) expression-related attributes refer to the attributes related to expressions in the following form:
VaR regx =/myexpr /;
VaR rsw.regx.exe C (STR );
There are two attributes related to the regx of the expression itself and three attributes related to the rs of the expression matching result. The following describes the attributes one by one.
2) two attributes related to the expression itself:

1> lastindex: return the start position of the next match. Note that the lastindex will continuously return the next matching value only when it must be a global match (with the G parameter in the expression, otherwise, this value always returns the first matching position, for example:
VaR regx =/user/D /;
VaR rs1_regx.exe C ("sdsfuser1dfsfuser2 ");
VaR lastindex1 = regx. lastindex;
Rsw.regx.exe C ("sdsfuser1dfsfuser2 ");
VaR lastindex2 = regx. lastindex;
Rsw.regx.exe C ("sdsfuser1dfsfuser2 ");
VaR lastindex3 = regx. lastindex;
The preceding lastindex1 is 9, the second lastindex2 is 9, and the third is 9. If regx =/user/D/g, the first is 9, and the second is 18, the third is 0.

2> Source: returns the expression string itself. For example:
VaR regx =/user/D /;
VaR rs1_regx.exe C ("sdsfuser1dfsfuser2 ");
VaR source = regx. source;
The source value is user/d.
3) three attributes related to the matching result:

1> index, returns the current matched position. For example:
VaR regx =/user/D /;
VaR rs1_regx.exe C ("sdsfuser1dfsfuser2 ");
VaR index1 = Rs. index;
Rsw.regx.exe C ("sdsfuser1dfsfuser2 ");
VaR index2 = Rs. index;
Rsw.regx.exe C ("sdsfuser1dfsfuser2 ");
VaR index3 = Rs. index;
Index1 is 4, index2 is 4, and index3 is 4. If the expression is added with the parameter g, index1 is 4, index2 is 13, and index3 reports an error (index is empty or not an object ).

2> input, used for matching strings. For example:
VaR regx =/user/D /;
VaR rs1_regx.exe C ("sdsfuser1dfsfuser2 ");
VaR input = Rs. input;
The input value is sdsfuser1dfsfuser2.

3> [0]: returns the first matching value in the matching result. For match, a number with multiple values may be returned, except for [0, [1], [2], and so on. For example:
VaR regx =/user/D /;
VaR rs1_regx.exe C ("sdsfuser1dfsfuser2 ");
VaR value1 = Rs [0];
Rsw.regx.exe C ("sdsfuser1dfsfuser2 ");
VaR value2 = Rs [0];
The value of value1 is user1, and the value of value2 is user2

5. Practical Application

1) Practical application 1
Description: There is a form with a "User Name" input field.
Requirements: Chinese characters, and no less than 2 Chinese characters, no more than 4 Chinese characters.
Implementation:
<SCRIPT>
Function checkform (OBJ ){
VaR username = obj. username. value;
VaR regx =/^ [/u4e00-/u9fa5] {2, 4} $/g
If (! Regx. Test (username )){
Alert ("invalid username !");
Return false;
}
Return true;
}
</SCRIPT>
<Form name = "myform" onsubmit = "Return checkform (this)">
<Input type = "text" name = "username"/>
<Input type = "Submit" vlaue = "Submit"/>
</Form>
2) Practical application 2
Description: If a string containing HTML tags is specified, the HTML Tag must be removed.
Implementation:
<SCRIPT>
Function toplaintext (htmlstr ){
VaR regx =/<[^>] *> | </[^>] *>/GM;
VaR STR = htmlstr. Replace (regx ,"");
Return STR;
}
</SCRIPT>
<Form name = "myform">
<Textarea id = "htmlinput"> </textarea>
<Input type = "button" value = "Submit" onclick = "toplaintext (document. getelementbyid ('htmlinput'). Value"/>
</Form>

Iii. Summary

1. Javascript Regular Expressions. I think there should not be many users among general programmers, because the pages we process are not very complex, the complicated logic is generally completed in the background. However, the current trend has been reversed, and rich clients have been accepted by more and more people, while Javascript is the key technology. For complex client logic, the role of regular expressions is also crucial, and it is also one of the important technologies that JavaScript experts must master.

2. In order to make it easier for you to have a more comprehensive and profound understanding of the content described above, I will systematically summarize some of the key points and content that may easily be confused, this part is critical!
Conclusion 1: The attachment parameter G usage
After the expression is added with the parameter G, it indicates that global matching can be performed. Note the meaning of "yes" here. Details:
1) if G is not added to the exec method of the expression object, only the first match is returned, regardless of the number of executions. If G is added, the first match is returned for the first execution, and the second match is returned for the execution, and so on. For example
VaR regx =/user/D /;
VaR STR = "user18dsdfuser2dsfsd ";
VaR rs1_regx.exe C (STR); // The RS value is {user1}
VaR rs21_regx.exe C (STR); // The RS value is still {user1}
If regx =/user/D/g; then the RS value is {user1}, and the rs2 value is {user2}
This example shows that for the exec method, if the expression is added with G, it does not mean that the exec method can return all the matches, but after G is added, I can get all the matching results in some way. The "method" here is to execute this method in sequence for exec.
2) adding G to the test method of the expression object without adding G is no difference.
3) for the match method of the string object, if G is not added, only the first match is returned. If the match method is always executed, the first match is always returned. If G is added, returns all matches at a time (note that this is different from the exec method of the expression object. For exec, the expression does not return all matches at a time even if G is added ). For example:
VaR regx =/user/D /;
VaR STR = "user1sdfsffuser2dfsdf ";
VaR rs = Str. Match (regx); // The RS value is {user1}
VaR rs2 = Str. Match (regx); // The RS value is still {user1}
If regx =/user/D/g, the RS value is {user1, user2}, and rs2 value is also {user1, user2}
4) for the replace method of the string object, if the expression is not added with G, only the first match is replaced. If G is added, all matches are replaced. (The three test questions at the beginning can illustrate this point)
5) for the split method of the string object, adding G is the same as without g, that is:
VaR Sep =/user/D /;
VaR array = "user1dfsfuser2dfsf". Split (SEP );
Then the array value is {dfsf, dfsf}
In this case, SEP =/user/D/g and the return value is the same.
6) for the search method of the string object, the same is true if G is not added.
Conclusion 2: Use of the additional parameter m
The Parameter m is appended to indicate that multi-row matching can be performed, but this only works when the ^ and $ modes are used. In other modes, you can perform multi-row matching without adding M (the multi-row string is also a common string ).
1) example of using ^
VaR regx =/^ B./g;
VaR STR = "bd76 dfsdf
Sdfsdfs dffs
B76dsf sdfsdf ";
VaR rs = Str. Match (regx );
At this time, if G is added or G is not added, only the first match {BD} is returned. If regx =/^ B. /GM, then all matches {BD, B7} are returned. Note that if regx =/^ B. /m, then only the first match is returned. Therefore, adding M indicates that multi-row matching can be performed, and adding G indicates that global matching can be performed.
2) examples of using other modes, such
VaR regx =/user/D /;
VaR STR = "sdfsfsdfsdf
Sdfsuser3 dffs
B76dsf user6 ";
VaR rs = Str. Match (regx );
If the parameter G is not added, {user3} is returned. If the parameter G is added, {user3, user6} is returned.
3) For m, we need to know how to use it. Remember that it only works for the ^ and $ modes. In these two modes, the role of M is: If M is not added, then the matching can only be performed on the first row. If M is added, the matching can be performed on all rows. Let's look at another ^ example.
VaR regx =/^ B ./;
VaR STR = "ret76 dfsdf
Bjfsdfs dffs
B76dsf sdfsdf ";
VaR rs = Str. Match (regx );
At this time, the RS value is null. If G is added, the RS value is still null. If M is added, the RS value is {BJ} (that is, the match is not found in the first line. Because the Parameter M exists, you can continue to find the following row to see if there is a match.) If both M and G are added, {BJ is returned, b7} (only adding m without g description, you can go to multiple rows for matching, but after finding a match, return. Adding G indicates that all the matches in multiple rows are returned, of course, this is true for the match method. For exec, it must be executed multiple times to return results in sequence)
Conclusion 3: In the textarea input field of HTML, press an enter key and the corresponding control character is "/R/N", that is, "Carriage Return" instead of "/n/R ", that is, "line feed and carriage return". Let's look at an example we mentioned earlier:
VaR regx =/A/R/NBC /;
VaR STR ="
BC ";
VaR rsw.regx.exe C (STR );
Result: The matching is successful. The RS value is: {}. If the expression is/A/n/rb/, It is not matched, therefore, in the General Editor, a "enter" key represents "Carriage Return", rather than "line feed", at least in the textarea domain.

Iv. Application Cases

1. Use Cases of Regular Expressions:
1) In the login scenario, check the user name entered by the user. Requirements:
The length must be between 6 and 18 characters.
The character must be a combination of letters, numbers, or underscores.
2) In the shopping scenario, some users may need to describe the product list (tags, bar codes, and unit prices) as follows:
You want to customize the font size of a description column in the product list, and keep the user's changes for the next login.
2. Let's take a look at the usage and implementation of regular expressions in the above two scenarios.
1) Implementation of some regular expressions in logon scenarios

2) Implementation of partial Regular Expressions in shopping scenarios
1> Requirement Analysis: You want to customize the font size of a column in the product list, and keep the user's changes for the next login.

2> Program Design

Class diagram:

3> code implementation
Pagesetting class

Pagesettingparser class

Related Article

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.