Before reading this article, please read the following question:
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]?
This problem is said to be convinced that the company's a pen test, but also cause me to write today's article, 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 argument is 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/);
All three of the above results are returned ["4"]
The 2.exec and match return 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 attribute
Alert (p.exec (str))
Alert (Str.match (p))
Are all "at". exec is equivalent to match on this occasion.
But if the regular expression is a global match (G property), then the above code results are different:
var str= "Cat,hat";
var p=/at/g; Note the G property
Alert (p.exec (str))
Alert (Str.match (p))
respectively is
"At"
"At,at".
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.
After the regular expression is calculated, each grouping is saved to a special place for future use. These special values stored in the grouping are referred to as reverse references.
A reverse reference is created and numbered in the order in which the opening parenthesis characters are encountered from left to right. For example, an expression (A? B? (C?))), will produce three reverse references numbered from 1-3:
No. 1: (A? ( B? (C?)))
No. 2: (B? ( C?))
Ref. 3: (C?)
The 4.match function will someone else the same function as exec if it satisfies the following conditions:
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?
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"
JavaScript exec Match Difference