Exec and match methods in Javascript

Source: Internet
Author: User

 

1) when using the match method, if G attribute is not specified, it is equivalent to the exec method of the Regexp object, rather than an array with only one element.

Example:

VaR STR = "Ahi"; var exp =/A (HI )/;

VaR arr1 = exp.exe C (STR); var arr2 = Str. match (exp); alert (arr1); // result: arr1.length = 2; arr1 [0] = AHI; arr1 [1] = Hi; alert (arr2 ); // result: arr2.length = 2; arr2 [0] = AHI; arr1 [1] = Hi; the result is the same as the preceding one.

 

 

2) at the same time, in the JS help document, if the exec method has an attribute g, set the starting position of the matching of the object to the character position next to the matching substring, when exec is called for the second time, it is retrieved from the character position indicated by lastindex. Using this feature, you can call exec repeatedly to traverse all the matches. This is equivalent to the case where the match has the G attribute (Actually, the matching result is put into the matches set.).

Example:

A) when the attribute g exists, the index and lastindex are updated to play a role in the next search:

Function regexptest () {var src = "The rain in Spain falls mainly in the plain."; var Re =/(\ W +)/g; // create the regular expression mode. VaR arr; while (ARR = re.exe C (SRC ))! = NULL) {document. write (ARR. index + "-" + Regexp. lastindex + "\ t" + arr [0]); // Regexp. lastindex and ARR. lastindex has the same attributes and can be exchanged. Note that the lastindex of IE6 and 7 is set to 0 again.

}};

Regexptest ();

// The above example can traverse the matched content. The index and lastindex of each small match can be obtained;

B) if the above example does not contain g, In the above example, the exec method does not update the global attribute (index, lastindex, etc.) of the Regexp object, and the above example will be in an endless loop, index and lastindex are always 0 and 3

 

It can be seen that in the exec process, attribute g can change the values of index and lastindex to facilitate the next retrieval location. The match method does not have this capability.

 

Attributes such as index and lastindex (the help includes leftcontext, rightcontext, lastmatch, and lastparen (the last parenthesis), but these attributes are based on index and lastindex ).

1) read-only attribute.

Example:

VaR src = "The rain in Spain falls mainly in the plain."; var Re =/(\ W +)/g; // create the regular expression mode. VaR arr; arr = re.exe C (SRC); Regexp. lastindex = 0; Regexp. Index = 0; arr. lastindex = 0; arr. Index = 0;

Document. write (ARR. index + "-" + arr. lastindex + "\ t" + arr [0] + "************" + Regexp. index + "-" + Regexp. lastindex + "\ t" + arr [0]);

// The result is 0-0 The ********** 0-3.

The reason is that the Regexp attribute is read-only. Even if the JS language is flexible, you can repair any attribute or add any attribute, and no syntax error is reported. However, the Regexp attribute cannot be changed, but the arrary object can be changed. However, every time you execute exec, attributes such as Regexp. Index will be assigned a new value to the returned arrary object.

For example:

VaR src = "The rain in Spain falls mainly in the plain."; var Re =/(\ W +)/g; // create the regular expression mode. VaR arr; arr = re.exe C (SRC); Regexp. lastindex = 0; Regexp. Index = 0; arr. lastindex = 0; arr. Index = 0;

Document. write (ARR. index + "-" + arr. lastindex + "\ t" + arr [0] + "************" + Regexp. index + "-" + Regexp. lastindex + "\ t" + arr [0]);

// The index attribute of the second arr execution will be updated. In fact, when the Regexp object instance executes the exec method, it will update the global Regexp. Index and ARR index.

Arr = re.exe C (SRC); document. write ("<br/>" + arr. index + "-" + arr. lastindex + "\ t" + arr [0] + "************" + Regexp. index + "-" + Regexp. lastindex + "\ t" + arr [0]);

// 0-0 The ********* 0-3 The // 4-8 rain *********** 4-8 rain

2) When different Regexp instance objects cross execute exec, attributes such as index and lastindex do not affect each other. Each time the exec or string match method is executed, a new value is assigned to rexexp. Index and so on. (This is actually necessary, but I am confused about it. I am wrong to understand it, mainly because "Regexp. lastindex = 0; "can be assigned a value, but the result does not change when the value is set, which makes my head messy .)

 

At first, I thought that if two Regexp objects cross-Execute exec, the index may be cleared. Because I think the index attribute is saved on the Global static attribute of Regexp. It is found that it is stored in a specific Regexp instance. Each time you execute exec or the string match method, a new value will be assigned to rexexp. Index and so on.

Haha, this may be a common mistake that people get used to the ideas of classes and class instances in C and Java think Regexp is a class, and Regexp. index is a static attribute of a class. This is true, but its value is updated by the regular object when the exec and string match methods are executed.

Example:

VaR src = "The rain in Spain falls mainly in the plain."; var RE1 =/(\ W +) //; // create the regular expression mode. VaR re2 =/(\ W +)/g; // create the regular expression mode. VaR arr;

Arr = re1.exec (SRC); document. write ("R1 first execute Exec:" + Regexp. index + "-" + Regexp. lastindex + "\ t" + arr [0]); arr = re2.exec (SRC); document. write ("<br/> R2 first execute Exec:" + Regexp. index + "-" + Regexp. lastindex + "\ t" + arr [0]); arr = re1.exec (SRC); document. write ("<br/> R1 executes Exec for the second time:" + Regexp. index + "-" + Regexp. lastindex + "\ t" + arr [0]);

Arr = re2.exec (SRC); document. write ("<br/> R2 second execution Exec:" + Regexp. index + "-" + Regexp. lastindex + "\ t" + arr [0]);

The output result is as follows:

R1 first execute Exec: 0-3 The R2 first execute Exec: 0-3 The R1 second execute Exec: 0-3 The R2 second execute Exec: 4-8 rain

 

3) The Match Method of the string object cannot obtain the index and lastindex of the object to be searched in the middle as the exec method, that is, it is one-time. That is, the location of the next search cannot be obtained. The match method can only obtain the last search, index, and lastindex when setting the G attribute. When the match method does not set the G attribute, obtain only the first matched index and lastindex.

Example:

A)

VaR src = "The rain in Spain falls mainly in the plain."; var Re =/\ W +/g; // has the G attribute. VaR I = 0; while (I ++ <10) {arr = SRC. match (re); document. write (Regexp. index + "-" + Regexp. lastindex + "\ t" + arr + "<br/> ");

}

// The result is as follows:

38-43 The, rain, in, Spain, falls, mainly, in, the, plain 38-43 The, rain, in, Spain, falls, mainly, in,, plain 38-43 The, rain, in, Spain, falls, mainly, in, the, plain 38-43 The, rain, in, Spain, falls, mainly, in,, plain 38-43 The, rain, in, Spain, falls, mainly, in, the, plain 38-43 The, rain, in, Spain, falls, mainly, in,, plain 38-43 The, rain, in, Spain, falls, mainly, in, the, plain 38-43 The, rain, in, Spain, falls, mainly, in,, plain 38-43 The, rain, in, Spain, falls, mainly, in, the, plain 38-43 The, rain, in, Spain, falls, mainly, in,, plain

 

B)

VaR src = "The rain in Spain falls mainly in the plain."; var Re =/\ W +/; // No G attribute. VaR I = 0; while (I ++ <10) {arr = SRC. match (re); document. write (Regexp. index + "-" + Regexp. lastindex + "\ t" + arr + "<br/> ");

} // The result is as follows:

0-3 The 0-3 The 0-3 The 0-3 The 0-3 The 0-3 The 0-3 The 0-3 The 0-3 The 0-3

C)

VaR src = "The rain in Spain falls mainly in the plain. "; var Re =/\ W +/g; var I = 0; arr = SRC. match (re); While (ARR [I]! = NULL) {document. write (Regexp. index + "-" + Regexp. lastindex + "\ t" + arr [I] + "<br/>"); I ++;} // The result is as follows:

38-43 the 38-43 rain 38-43 in 38-43 Spain 38-43 falls 38-43 mainly 38-43 in 38-43 the 38-43 Plain

Final conclusion (correct if any ):

1) exec is the Regexp object method, and match is the string object method.

2) If no result is found, both return NULL.

3) only when the regular expression must specify the global G attribute can match be returned. Otherwise, the results of the match and exec methods are equivalent.

4) exec always returns information related to the first match. The first value of the returned array is the first matched string, the rest is the reverse reference of all groups (that is, the matching content of the parentheses);

5) After exec sets the G attribute, although the matching result is not affected by G, the returned result is still an array (the first value is the first matched string, but it will change the index and lastindex values, and set the starting position of the matching of the object to the character position next to the matching substring, when exec is called for the second time, it is retrieved from the character position indicated by lastindex. Similarly, after the G attribute is set for the match method, the values of index and lastindex are also changed, but the value is one-time. It is impossible to accumulate by process like exec (the result is put into the matches set), so it is impossible to accumulate the location for the next retrieval.

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.