This article is mainly on the JS regular expression exec and match the difference between the introduction, the need for friends can come to the reference, I hope to help you.
Regular expression rules some rules are no longer stated here, only the difference between exec and match is recorded:
1, Exec is the method of the regular expression, not the method of the string, its parameter is the string, as follows:
As defined above
var reg = new RegExp ("abc");
var str = "3ABC4,5ABC6";
Reg.exec (str);
2. Match is a string that performs a method of matching regular expression rules, and his arguments are regular expressions, such as
var reg = new RegExp ("abc");
var str = "3ABC4,5ABC6";
Str.match (REG);
3, exec and match return are arrays;
If the regular expression executed by Exec does not have a subexpression (the contents of parentheses, such as/ABC (\s*)/(\s*)), if there is a match, returns the first matched string content, at which point the array has only one element, and if no match returns null;
var reg = new RegExp ("abc");
var str = "3ABC4,5ABC6";
Alert (reg.exec (str));
Alert (Str.match (reg));
Execute the above code, and you'll see that both things are the same: ABC,
4. If you define a regular expression object as a global match, such as:
var reg = new RegExp ("abc", "G");
var str = "3ABC4,5ABC6";
Alert (reg.exec (str));
Alert (Str.match (reg));
is ABC and ABC,ABC; Because match executes a global match query, and exec does not have a subexpression that only finds a match that returns.
5, when the expression contains sub-expressions in the case:
var reg = new RegExp ("A (BC)");
var str = "3ABC4,5ABC6";
Alert (reg.exec (str));
Alert (Str.match (reg));
You will find that the results of both executions are: ABC,BC;
6. When a regular expression object is defined as a global match
var reg = new RegExp ("A (BC)", "G");
var str = "3ABC4,5ABC6";
Alert (reg.exec (str));
Alert (Str.match (reg));
The result of both returns is ABC,BC and ABC,ABC,
Summarized as:
1, when the regular expression has no sub-expression, and is defined as a non-global match, the result of exec and match execution is the same, all return the first matching string content;
2, when the regular expression has no sub-expression, and defined as a global match, exec and match execution, do exist multiple matches, then match returns a plurality of element array;
3. When a regular expression has a child representation and is defined as a non-global match, the result of exec and match execution is the same as the 5th case above;
4, when the regular expression has a child representation, and is defined as a global match, exec and match execution results are different, at this time match will ignore the sub-expression, only find the full match regular expression and return all content, such as the 6th case;
It also says that exec has no relationship with global definition, while match is globally associated, and when defined as non-global, the results are the same
Description of the difference between the JavaScript regular expression exec () and match ()