Create a regular expression
VaR p1 = new Regexp ("ABC", "IgM"); var P2 = Regexp ("ABC", "IgM"); var P3 =/ABC/IgM; // determine whether it is a regular expression object alert (P1 instanceof Regexp); // truealert (P2 instanceof Regexp); // truealert (P3 instanceof Regexp); // true
Five attributes of a regular expression object
VaR P =/ABC/IgM; // Source: expression text, read-only alert (P. source); // ABC // ignorecase: whether the specified I is available, read-only; I indicates not case sensitive alert (P. ignorecase); // true // Global: whether the specified g exists. Read-Only; G indicates that all Alert (P. global); // true // multiline: Specifies whether M is available, read-only; M indicates multi-row matching alert (P. multiline); // true // lastindex: The best matching position, which is a read/write attribute. alert (P. lastindex); // 0; it must be 0 before matching
Test of Two Methods of a regular expression object
// This test is very simple. It can only return true/false to indicate whether a match is found. It does not know more information about var STR, P, B; STR = '1: ABC; 2: ABC; 3: ABC; 4: ABC; 5: ABC; 6: ABC; 7: ABC; 8: ABC '; P =/ABC/I; B = P. test (STR); alert (B); // true; indicates that there is a match to P =/abcdefg/I; B = P. test (STR); alert (B); // false; indicates that no matching is found. // for test, it is meaningless to specify g (Global match; it returns the result if it finds it.
Exec of the two methods of the regular expression object
// Exec returns the matching result, which is an array var STR, P, arr; STR = '1: ABC; 2: ABC; 3: ABC; 4: ABC; 5: ABC; 6: ABC; 7: ABC; 8: abc'; P =/ABC/I; arr = p.exe C (STR); alert (ARR ); // ABC // how can I know that the returned arr is an array? Test: Alert (ARR instanceof array); // truealert (ARR [0]); // ABC // Why use array expression? Because regular expressions may contain "subexpressions", such as P =/(AB) (C)/I; // subexpressions are distinguished, the above expression contains two subexpressions. When used for the exec function, an array with length = 3 will be returned: // arr [0]: matching the result of the entire expression; // arr [1]: match the result of the first subexpression; // arr [2]: match the result of the second subexpression... // example containing a subexpression (continue using the above string Str) P =/(AB) (C)/I; arr = p.exe C (STR); alert (ARR ); // ABC, AB, C // traverse this array for (VAR I = 0; I
Global matching of Exec
// First, exec will not be like string. match is so convenient to complete global matching; however, this is just its strength. // In the first example, G is not specified. The result of executing three exec operations is the same var STR, P1, arr; STR = '1: ABC; 2: ABC; 3: ABC; 4: ABC; 5: ABC; 6: ABC; 7: ABC; 8: ABC '; P1 =/(AB) (C)/I; arr = p1.exec (STR ); alert (ARR); // ABC, AB, Carr = p1.exec (STR); alert (ARR); // ABC, AB, Carr = p1.exec (STR ); alert (ARR); // ABC, AB, calert (p1.lastindex); // 5 // in this example, global match is used and the results are different. var STR, P2, arr; STR = '1: ABC; 2: ABC; 3: ABC; 4: ABC; 5: ABC; 6: ABC; 7: ABC; 8: ABC '; P2 =/(AB) (C)/ig; arr = p2.exec (STR); alert (ARR); // ABC, AB, Carr = p2.exec (STR); alert (ARR); // ABC, AB, Carr = p2.exec (STR); alert (ARR); // ABC, AB, calert (p2.lastindex); // 17 // You can traverse all the matches as follows: var STR, P, arr; STR = '1: ABC; 2: ABC; 3: ABC; 4: ABC; 5: ABC; 6: ABC; 7: ABC; 8: ABC '; P =/(AB) (C)/ig; while (ARR = p.exe C (STR ))! = NULL) {alert (ARR); // The matching result of the subexpression can be extracted here. This function is only available for Exec .}