Use of the regular expression match, exec, and test
Match and Exec return an array when matching succeeds, and all returned when no match is null;test returns true if the match succeeds and false if no match is returned. Match is a method of string, while test and exec are regexp methods. This paper discusses the problem of global matching and packet matching, and has source attachment.
First, the global match
1. Regular expressions can be defined using the RegExp object, or they can be defined using a constant expression.
var regexp=New regexp (/\w+/gi); or var regexp=/w+/gi;
2. (1) When a global variable is not used, the match and Exec method are the same, and each time the string that needs to be matched is matched for the first time.
1 varRe =NewREGEXP (/([a-z]+) = (\d+)/i); 2 varstr = "cs=2013 kd=2015";3document.write ("Exec:" +re.exec (str) + "<br/>");4document.write ("Match:" +str.match (RE) + "<br/>");5document.write ("Test:" +re.test (str) + "<br/>"); 6document.write ("<br/><br/>"); 7document.write ("Exec:" +re.exec (str) + "<br/>");8document.write ("Match:" +str.match (RE) + "<br/>");9document.write ("Test:" +re.test (str) + "<br/>");
Result output:
(2) When using global variables, exec is not the same as match. exec executes each execution from the point after the last execution, so it is different, and the result of each execution of the match is the same.
1 varRe =NewREGEXP (/([a-z]+) = (\d+)/gi); 2 varstr = "cs=2013 kd=2015";3document.write ("Exec:" +re.exec (str) + "<br/>");4document.write ("Match:" +str.match (RE) + "<br/>");5document.write ("Test:" +re.test (str) + "<br/>"); 6document.write ("<br/><br/>"); 7document.write ("Exec:" +re.exec (str) + "<br/>");8document.write ("Match:" +str.match (RE) + "<br/>");9document.write ("Test:" +re.test (str) + "<br/>");
Result output:
Second, group
When there are no global matching groupings, match and exec return the same results. Since the regular expression is grouped in parentheses, all the groupings of the result are returned, in turn, while the matching result is returned. The result of Exec is not the same as the result of the match, and match returns all the matched results once in the array, and exec matches in order, so you need to use the while loop to do the output once. The result of the array subscript access to the exec return result is that each complete match returns an array, followed by the [complete sequence, subsequence 1, sub-sequence 2, ... , sub-sequence n], all Exec[0] is the complete sequence, and only the () subsequence in parentheses. In this example, exec is better than the match effect, in that it is possible for exec to directly partition both sides of the equal sign, i.e.: exec[1]=exec[2], which can be accessed directly. Match is more troublesome and can be separated by match[0].split ("=") of two sequences, while Match[0][0] only has one letter, not the actual semantic block.
1 varstr = "? id=21&rid=4&page=5";2 varregexp=/([a-z]+) = (\d+)/IG;3 varMatch =Str.match (regexp);4 5 for(vark=0;k<match.length;k++){6document.write (match[k]+ "<br/>");7 }8document.write ("<br/><br/><br/><br/>");9 Ten varI=1; One while(exec=regexp.exec (str)) { Adocument.write ("+i+" Times: "+exec+": "+exec[0]+"----> "+exec[1]+" <br/> "); -i++; -}
Results
Use of the "JavaScript" regular expression match, exec, and test