The difference of Test,exec,match method in JS regular expression
Test
Test returns a Boolean to find whether the pattern exists in the corresponding string.
var str = "1a1b1c";
var reg = new RegExp ("1.", "");
Alert (Reg.test (str)); True
Exec
Exec finds and returns the current matching result, and returns it as an array.
var str = "1a1b1c";
var reg = new RegExp ("1.", "");
var arr = reg.exec (str);
If no pattern exists, arr is null, otherwise arr is always an array of length 1 whose value is the current match. ARR also has three properties: the position of the index current match, the position at which the current match ends (index + the length of the current match), and input in the example above is str. lastIndex.
The Exec method is affected by the parameter G. If you specify G, the next time you call Exec, you will start looking for the last matching lastIndex.
var str = "1a1b1c";
var reg = new RegExp ("1.", "");
Alert (reg.exec (str) [0]);
Alert (reg.exec (str) [0]);
Both of these outputs are 1a. Now look again at the specified parameter g:
var str = "1a1b1c";
var reg = new RegExp ("1.", "G");
Alert (reg.exec (str) [0]);
Alert (reg.exec (str) [0]);
The first output above 1a, the second output 1b.
Match
Match is a method of a String object.
var str = "1a1b1c";
var reg = new RegExp ("1.", "");
Alert (Str.match (reg));
Match this method is a bit like exec, but: Exec is the method of the RegExp object, and math is the method of the String object. The two also have a different point, that is, the interpretation of the parameter G.
If the parameter g is specified, match returns all results at once.
var str = "1a1b1c";
var reg = new RegExp ("1.", "G");
Alert (Str.match (reg));
Alert (Str.match (reg)); This sentence is the same as the result of the same sentence
The result is an array of three elements: 1a, 1b, 1c.
Regular expressions are often used in JavaScript, and regular expressions often use the match and test functions, and, of course, exec. Here's a code example to tell the difference between them.
Match Example
Copy CodeThe code is as follows:
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
var regexp =/[a-e]/gi;
var rs = Str.match (regexp);
rs= Array (' A ', ' B ', ' C ', ' d ', ' E ', ' A ', ' B ', ' C ', ' d ', ' e ');
Test Example
Copy CodeThe code is as follows:
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
var regexp =/[a-e]/gi;
var rs = regexp.test (str);
rs = true; Boolean
EXC Example
Copy CodeThe code is as follows:
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
var regexp =/[a-e]/gi;
var rs;
while ((rs = regexp.exec (str)) = NULL)
{
document.write (RS);
document.write (Regexp.lastindex);
document.write ("<br/>");
}
OUTPUT
---------------------------------
A 1
B 2
C 3
D 4
E 5
A 27
B 28
C 29
D 30
E 31
Another EXC Example
Copy CodeThe code is as follows:
var regexp =/ab*/g;
var str = "Abbcdefabh";
var rs;
while ((rs = regexp.exec (str)) = NULL)
{
document.write (RS);
document.write (Regexp.lastindex);
document.write ("<br/>");
}
OUTPUT
---------------------------------
ABB 3
AB 9
The difference of Test,exec,match method in JS regular expression