The exec () and Match () methods of JavaScript

Source: Internet
Author: User
Tags first string

Reproduced from others ' blogs, not original

A thorough understanding of the Exec and match methods in JavaScript

Before reading this article, please read the following question:

Title 17:read The following JavaScript code:

var sometext= "web2.0. net2.0";
var pattern=/(\w+) (\d) \. (\d)/g;
var outcome_exec=pattern.exec (Sometext);
var outcome_matc=sometext.match (pattern);

What is outcome_exec[1] and outcome_matc[1]?

Choice A:true
Choice B:false
Choice C:null
Choice D:web
Choice e:web2.0
Choice f:undefined
Choice g:net2.0

This problem is said to be an IT company's pen test, but also cause me to write this article today, but the topic I slightly modified a bit, if you answer the question correctly, you can not look below.

-----------------------------------------------------------------

The function of matching strings related to regular expressions in JavaScript is mainly the method of the RegExp class exec (String) and the method of the String class match (regex), and of course there are other methods that are not discussed here, But maybe a lot of programmers confuse exec and match, and here are the key features of both:

  1. Exec is a method of a regular expression, not a method of a string, and its arguments are the string, as follows:

    var re=new RegExp (/\d/);
    Re.exec ( "abc4def");

    Or use the Perl style:

    /\d/.exec ( "abc4def");

    Match is the method provided by the string class, and its arguments are regular expression objects, and the following usage is correct:

    "Abc4def". Match (\d);
  2. the exec and match returns are all arrays

    If the regular expression that executes the Exec method is not grouped (without parentheses), then if there is a match, he will return an array with only one element, the only element of which is the first string that matches the regular expression, or null if there is no match.

    The following two alert functions pop up with the same message:

    var str= "Cat,hat";
    var p=/at/; // No G property
    Alert (p.exec (str))
    Alert (Str.match (p))

    is "at". exec is equivalent to match on this occasion.

    But if the regular expression is a global match (G property), the above code results are different:

    var str= "Cat,hat";
    var p=/at/g; //Note G Property
    Alert (P.exec (str))
    Alert (Str.match (p))

    is
    "at"
    "At,at", respectively.

    Because exec always returns only the first match, and match returns all matches when the G attribute is specified in the regular.
     
  3. exec if a match is found and contains a grouping, the returned array will contain more than one element, the first element being the match found, followed by the first and second of the matching elements. Group (reverse reference)

    The following code will pop up "Cat2,at":

    var str= "Cat2,hat8";
    var p=/c (at) \d/;
    Alert (p.exec (str))

    Where the first element is the matching string "Cat2", the following element is the "at" in parentheses.
  4. The match function implements the same functionality as exec when it meets the following conditions: someone else

    1, the regular expression contains the grouping (parentheses)
    2. Returns a unique match

    And look at the following code:

    var str= "Cat2,hat8";
    var p=/c (at) \d/;
    Alert (p.exec (str))
    Alert (Str.match (p))

    Will pop up the message "Cat2,at", does it feel strange?

The following links provide some demos: http://www.webchat.com.cn/exec_match.htm

Now let's review the questions raised at the beginning of the article:

var sometext= "web2.0. net2.0";
var pattern=/(\w+) (\d) \. (\d)/g;
var outcome_exec=pattern.exec (Sometext);
var outcome_matc=sometext.match (pattern);

Analysis:

The value of Outcome_exec: the G attribute in pattern has no effect on the EXEC function, so exec matches the first string "web2.0" that can be matched, as the first element of its return array, and because the pattern contains three groupings ((\w+), (\d), (\d)), so the array will also contain three elements, followed by "Web", "2", "0", so the final result of the exec execution is: ["web2.0", "Web", "2", "0"]

OUTCOME_MATC value: Because pattern is globally matched, match matches all strings that can be matched, so the value of the resulting array is OUTCOME_MATC ["web2.0", "net2.0"]. If the pattern does not have a G attribute, it will be the same as the outcome_exec result, because it conforms to the condition described in section 4th of this article: there is a grouping and returns a unique match!

Summarize:

Match is an array that returns all matching string compositions, but the regular expression must specify the global G property to return all matches, and the G property will return an array of only one element.

exec always returns information related to the first match, and its return array includes the first matched string, and all the grouped reverse references.

-------------------------------------------

In some cases, EXEC returns the same result as the match returns:

var str= "Cat,hat";
var p=/at/; // no g attribute
Alert (p.exec (str))
Alert (Str.match (p))

All pop Up "at"

-------------------------------------------

In some cases, match returns the same result as the Exec returns:

var str= "Cat2,hat8";
var p=/c (at) \d/;
Alert (p.exec (str))
Alert (Str.match (p))

All pop Up "Cat2,at"

The difference between the exec and match methods has been somewhat confusing, and today I look at the Help document (Help document mainly based on some obscure examples to support the understanding), and then in Baidu collected the next few introductory articles, one of the following article (Building Orchid Wind ... "Thorough understanding of the Exec and match method in JavaScript" was retrieved several times and searched for the first one in Baidu. But after reading, do some examples, found that the author's conclusions some problems, corrected, to avoid misleading you cheese.

1. Before reading the article, quoting the classic interview question, if you know the answer to the question, then there is no need to look down.

var sometext= "web2.0. net2.0";
var pattern=/(\w+) (\d) \. (\d)/g;
var outcome_exec=pattern.exec (Sometext);
var outcome_matc=sometext.match (pattern);

What is outcome_exec[1] and outcome_matc[1]?

Choice A:true
Choice B:false
Choice C:null
Choice D:web
Choice e:web2.0
Choice f:undefined
Choice g:net2.0

Think about 1 minutes ing .....

Some knowledge is useless, think two days also useless, so think not too long. Meet this problem directly Google and Baidu Bar!
2. Thinking is not finished, now read the original text and the conclusion of the text:

The author's original text reads:

Http://www.cnblogs.com/xiehuiqi220/archive/2008/11/05/1327487.html

Finally, the authors summarize the following conclusions:

A) match returns an array of all matching string compositions, but the regular expression must specify the global G property to return all matches, and the G property will return an array of only one element.

b) Exec always returns information related to the first match, and its return array includes the first matched string, and all the grouped reverse references.

3. Discover the problem:

3-1) The above conclusions are wrong. When using the match method, if you do not specify the G attribute, the Exec method with the RegExp object can be equivalent to an array of only one element.

Example:

var str= "Ahi";
var exp=/a (HI)/;

var arr1 = exp.exec (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; result ibid.

3-2) At the same time, in the JS Help document, when you execute the Exec method, if there is a property g, set the start position of the object's match to the character position immediately after the string, and when you call exec the second time, the search starts at the character position indicated by
Lastindex. Using this feature, you can call exec repeatedly to iterate through all matches, which is equivalent to the case where match has a G attribute ( is actually putting the result of the match into the Matches collection ).

Examples are as follows:

A) when there is a case of attribute G, 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; Creates a regular expression pattern.
var arr;
while (arr = re.exec (src)) = null) {
document.write (Arr.index + "-" + Regexp.lastindex + "\ T" + arr[0]);//Here Regexp.lastindex and Arr.lastindex have the same properties, which can be interchanged. Note the bug of lastindex Reset 0 in IE6 and 7

}
};

Regexptest ();

The above example can traverse the matching content. And can get each small match index and lastindex;

b) If the above example is not in the case of G, then the Exec method does not update the global properties of the RegExp object (index, lastindex, and so on), and the above example will fall into a dead loop, with index and lastindex always 0 and 3

The visible attribute g can change the value of index and lastindex during the exec process so that the next retrieval location, the match method does not have this ability.

4. For attributes such as index and lastindex (the help also includes Leftcontext, Rightcontext, Lastmatch, Lastparen (the last parenthesis), but these attributes are based on index and lastindex).

4-1) Read-only properties.

Here's an example:

var src = "The rain in Spain falls mainly in the plain.";
var re =/(\w+)/g; Creates a regular expression pattern.
var arr;
arr = re.exec (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.

The reason is that RegExp's properties are read-only, and even if the JS language is flexible, you can fix any attributes or add any attributes without reporting a syntax error. However, property changes still cannot be regexp, but arrary objects can be changed, but each time exec executes, the properties such as Regexp.index are re-assigned to the returned Arrary object.

For example:

var src = "The rain in Spain falls mainly in the plain.";
var re =/(\w+)/g; Creates a regular expression pattern.
var arr;
arr = re.exec (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 property of the second arr is updated, in fact, the RegExp object instance updates the global Regexp.index and ARR's index when executing the Exec method, and in the following section

arr = re.exec (SRC);
document.write ("<br/>" +arr.index + "-" + Arr.lastindex + "\ T" + arr[0]+ "**********" +regexp.index + "-" + regexp.last Index + "\ T" + arr[0]);

0-0 the**********0-3 the
4-8 rain**********4-8 Rain

4-2) When different regexp instance objects cross exec, index, lastindex and other attributes do not affect each other. Each time exec executes or executes a string's match method, the new value is given to Rexexp.index and so on. (This is actually necessary, but I am in this head a Fanhun, to understand wrong, mainly because "Regexp.lastindex = 0;" Can be assigned, but when the value is taken, the result does not change, which makes my head confused. )

At first I thought that if two RegExp objects were to cross exec, the index would be zeroed out. Because I think the index property is stored on the global static property of RegExp. Now found to be stored on a specific regexp instance, each time exec executes or executes a string's match method, the new value is given to Rexexp.index and so on.

Oh, this is probably the mistake of people who are accustomed to the idea of classes and class instances in C and Java, that REGEXP is a class, and that Regexp.index is a static property of a class. This is true, but his value is that it will be updated by the regular object when executing the match method of exec and string.

Examples are as follows:

var src = "The rain in Spain falls mainly in the plain.";

var Re1 =/(\w+)/; Creates a regular expression pattern.
var Re2 =/(\w+)/g; Creates a regular expression pattern.
var arr;

arr = re1.exec (SRC);
document.write ("R1 executes exec for the first time:" +regexp.index + "-" + Regexp.lastindex + "\ T" + arr[0]);

arr = re2.exec (SRC);
document.write ("<BR/>R2 executes exec for the first time:" +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 executes exec for the second time:" +regexp.index + "-" + Regexp.lastindex + "\ T" + arr[0]);

The results of the output are as follows:

R1 for the first time to execute exec:0-3 the
R2 for the first time to execute exec:0-3 the
R1 Second Execution exec:0-3 the
R2 Second execution exec:4-8 rain

4-3) The match method of the string object does not get the index and lastindex of the intermediate lookup object as the Exec method, that is, one-time. That is, the next retrieved location cannot be obtained, and the match method can only get the last retrieval when setting the G property, and Index and Lastindex;match only get the first matching index and lastindex when the G property is not set.

Examples are as follows:

A

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

}


The results are as follows:

38-43 The,rain,in,spain,falls,mainly,in,the,plain
38-43 The,rain,in,spain,falls,mainly,in,the,plain
38-43 The,rain,in,spain,falls,mainly,in,the,plain
38-43 The,rain,in,spain,falls,mainly,in,the,plain
38-43 The,rain,in,spain,falls,mainly,in,the,plain
38-43 The,rain,in,spain,falls,mainly,in,the,plain
38-43 The,rain,in,spain,falls,mainly,in,the,plain
38-43 The,rain,in,spain,falls,mainly,in,the,plain
38-43 The,rain,in,spain,falls,mainly,in,the,plain
38-43 The,rain,in,spain,falls,mainly,in,the,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 results are 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 the

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 results are 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

5. Final conclusion (please correct me if it is wrong):

1) Exec is the RegExp object method, and match is a string object method;

2) If no results are found, both return null;

3) match can return all matches only if the regular expression must specify the global G property, otherwise the match is equivalent to the result of the Exec method;

4) Exec always returns information related to the first match, whose return array the first value is the first matched string, and the rest is the inverse reference of all the groupings (that is, the matching content of the parentheses);

5) Exec after setting the G property, although the match result is not affected by G, the returned result is still an array (the first value is the first match to the string, the later is the grouping match), but will change the value of index and lastindex, etc. Sets the starting position of the object's match to the character position immediately following the string, and when exec is called the second time, it is retrieved from the character position indicated by lastindex. The same match method, after setting the G property, also changes the value of index and lastindex, but is one-time. It is not possible to accumulate in a process like exec (the result is placed in the Matches collection), so it is not possible to accumulate the next retrieved location.

Ps:

The answer to the question at the very beginning was D and G. Do you have any idea?

The above tests have been tested in IE and Firefox, and the results are consistent.

The premise of the above test is that JavaScript supports RegExp objects. Early browser JavaScript engines may not support regular objects or some properties of regular expression objects.

The exec () and Match () methods of JavaScript

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.