The exec () method of the JavaScript RegExp object

Source: Internet
Author: User

The exec () method of the JavaScript RegExp object is used to match the string, and its behavior is somewhat different from match ().

For Regexpobject.exec (), W3school is described above:

The exec () method is powerful, is a common method, and is more complex to use than the test () method and the method that supports the String object of a regular expression.

If EXEC () finds the matched text, it returns an array of results. Otherwise, NULL is returned. The No. 0 element of this array is the text that matches the regular expression, and the 1th element is the text that matches the 1th sub-expression of regexpobject (if any), and the 2nd element is the text (if any) that matches the 2nd sub-expression of regexpobject, in such Push. In addition to the array element and the length property, the Exec () method returns two properties. The Index property declares the position of the first character of the matched text. The input property holds the string that was retrieved. We can see that when calling the Exec () method of a non-global REGEXP object, the returned array is the same as the array returned by the calling Method String.match ().

However, when Regexpobject is a global regular expression, the behavior of exec () is slightly more complex. It will begin retrieving string strings at the character specified by the LastIndex property of Regexpobject. When exec () finds text that matches the expression, it sets the Regexpobject LastIndex property to the next position of the last character of the matched text after the match. This means that you can iterate through all the matching text in a string by calling the Exec () method repeatedly. When exec () can no longer find a matching text, it returns null and resets the LastIndex property to 0.

The exec () method of the RegExp object is easily confused with the match () method of the String object, as in the String.match () method:

The match () method retrieves the string stringobject to find one or more text that matches the regexp. The behavior of this method depends to a large extent on whether RegExp has a flag g.

If RegExp does not have a flag G, then the match () method can only perform a match in Stringobject. If no matching text is found, match () returns NULL. Otherwise, it returns an array that holds information about the matching text it finds. The No. 0 element of the array holds the matching text, while the rest of the elements hold the text that matches the subexpression of the regular expression. In addition to these regular array elements, the returned array contains two object properties. The Index property declares the position of the starting character of the matching text in Stringobject, and the input property declares a reference to Stringobject.

If RegExp has the flag G, the match () method performs a global retrieval and finds all matching substrings in the stringobject. If no matching substring is found, NULL is returned. If one or more matching substrings are found, an array is returned. However, the contents of the array returned by the global match are very different from the former, and its array elements hold all the matching substrings in the stringobject, and there is no index attribute or input property.

Note: In global retrieval mode, match () does not provide information about the text that matches the subexpression, nor does it declare the location of each matched substring. If you need these globally retrieved information, you can use Regexp.exec ().

Here are a few things to note:

1. Lastindex Properties

If Regexpobject in Regexpobject.exec () is a global mode, lastindex is set to the next position of the last character that matches after each match.

Incidentally, the RegExp object's exec () method and the test () method have this problem: if the RegExp object has a flag G or a global schema, the value of lastindex is the starting point for the next match. If you want to start retrieving a new string after a match, you must manually set the lastindex to 0.

The String.match () method does not exist for this problem.

2. Non-global mode

In non-global mode, the Exec () method behaves the same as the match () method. The result is an array, the No. 0 element is the matching text, the remaining elements are text that matches the subexpression (with parentheses), and two properties index and input.

var reg =/(DO[GP])/; var str = ' dog DOPG '//  ["Dog", "dog", index:0, Input: "Dog DOPG"]//  ["Do G "," dog ", index:0, Input:" Dog DOPG "]

3. Global mode

In global mode, the Exec () method matches the string starting at lastindex, and once the match succeeds, it returns an array of results, does not proceed with subsequent matches, and modifies the Lastindex property.

The match () method returns an array of all matching results.

Since the match () method can return an array of matching results directly, and the Exec () method returns only the first one, why do I need the EXEC () method?

Look at the following code:

var reg =/do ([GP])/g; var str = ' dog DOPG '//  ["Dog", "G", index:0, Input: "Dog DOPG"] ["DOP", "P", Index:4, INP UT: "Dog DOPG"]//  ["Dog", "DOP"]

In global mode, you can get each matching text by calling the Exec () method multiple times, and you can get the text that each subexpression matches, and every Lastindex,match () method cannot.

Finally, the text quoted in W3school as a summary:

Note that exec () adds the full details to the array it returns, regardless of whether the regexpobject is a global mode. This is where EXEC () differs from String.match (), which returns much less information in global mode. So we can say that calling the Exec () method repeatedly in a loop is the only way to get complete pattern matching information for the global schema.

The exec () method of the JavaScript RegExp object

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.