Exec is a method of regular expressions, not a method of strings, and its parameters are strings, as shown below:
var re=new RegExp(//d/);re.exec( "abc4def" );//or use perl style://d/.exec( "abc4def" );//match is the method provided by the string class. Its parameters are regular expression objects. The following usage is correct: "abc4def".match(/d);
Exec and match return arrays
If the regular expression that executes the exec method is not grouped (without brackets), then if there is a match, it will return an array with only one element, and the only element of this array is the first string that the regular expression matches; if there is no match, it will return null.
The following two alert functions pop up information is the same:
var str= "cat,hat" ;var p=/at/; //There is no g attribute alert(p.exec(str)) alert(str.match(p))
All are "at". In this case, exec is equivalent to match.
However, if the regular expression is globally matched (g attribute), the above code will have different results:
var str= "cat,hat" ;var p=/at/g; //Note the g attribute alert(p.exec(str)) alert(str.match(p))
They are
"at"
"at,at".
Because exec will always return only the first match, and match will return all matches when the g attribute is specified regularly.
If an exec finds a match and contains a group, the returned array will contain multiple elements. The first element is the found match, and the subsequent elements are the first, second... groupings in the match (reverse reference)
The following code will pop up "cat2,at":
var str= "cat2,hat8" ;var p=/c(at)/d/;alert(p.exec(str))
The first element is the matching string "cat2", and the subsequent element is the matching "at" in parentheses.
The match function will interfere with the following conditions and realize the same function as exec:
1. Regular expression contains groupings (brackets)
2. Return the unique match
Let's look at the following code:
var str= "cat2,hat8" ;var p=/c(at)/d/;alert(p.exec(str))alert(str.match(p))
The message "cat2,at" will pop up. Do you think it's very strange?
Summarize:
match is an array that returns all matching strings, but the regular expression must specify the global g attribute to return all matches. If the g attribute is not specified, an array with only one element will be returned.
exec always returns information related to the first match, which returns the array including the first match string, backreferences to all packets.
In some cases, the result returned by exec is the same as the result returned by match:
var str= "cat,hat" ;var p=/at/; //There is no g attribute alert(p.exec(str)) alert(str.match(p))
"at" pops up
In some cases, the result returned by match is the same as the result returned by exec:
var str= "cat2,hat8" ;var p=/c(at)/d/;alert(p.exec(str))alert(str.match(p))
"cat2,at" pops up
The above article in-depth analysis of the exec and match methods in JavaScript is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.