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:
- 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);
- 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:
varStr="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:
varStr="Cat,hat";
var p=/at/g; //Note 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.
- 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 in the match. 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 a matching string "Cat2", followed by an "at" in parentheses.
- 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?
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= < Span style= "Color:rgb (128,0,0)" > "Cat,hat" ;
var p=/at/; // No G property
Alert (p.exec (str))
Alert (Str.match (p))
pops 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"
Json2form, the industry's amazing form development framework, greatly improves the efficiency of your development information system. View Demo
A deep understanding of the similarities and differences between exec and match methods in JavaScript