SoFunction
Updated on 2025-02-28

Description of the difference between test, exec, match methods in js regular expression

Description of the difference between test, exec, match methods in js regular expression

test
test returns Boolean to find whether there is a pattern in the corresponding string.
var str = "1a1b1c";
var reg = new RegExp("1.", "");
alert((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 = (str);
If no pattern exists, arr is null, otherwise arr is always an array of length 1, and its value is the current match. arr has three properties: index The position of the current match; lastIndex The position of the current match ending (index + the length of the current match); input As in the above example, input is str.


The exec method is affected by the parameter g. If g is specified, the next time exec is called, the search will start from the last matching lastIndex.
var str = "1a1b1c";
var reg = new RegExp("1.", "");
alert((str)[0]);
alert((str)[0]);
Both outputs above are 1a. Now look at the specified parameter g:
var str = "1a1b1c";
var reg = new RegExp("1.", "g");
alert((str)[0]);
alert((str)[0]);
The first output 1a and the second output 1b are mentioned above.


match
match is a method of the String object.
var str = "1a1b1c";
var reg = new RegExp("1.", "");
alert((reg));
The match method is a bit like exec, but: exec is a method of the RegExp object; math is a method of the String object. There is another difference between the two, which is the explanation of parameter g.
If parameter g is specified, match returns all results at once.
var str = "1a1b1c";
var reg = new RegExp("1.", "g");
alert((reg));
//alert((reg)); // The result of this sentence is the same as the previous sentence
This result is an array with three elements, namely: 1a, 1b, and 1c.

Regular expressions are often used in JavaScript, and regular expressions are often used in two functions: Match and Test, and of course Exec. Here we use code examples to distinguish the differences between them.

Match Example

Copy the codeThe code is as follows:

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;
var rs = (regexp);
//rs= Array('A', 'B', 'C', 'D', 'E', 'a', 'b', 'c', 'd', 'e');

Test Example
Copy the codeThe code is as follows:

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;
var rs = (str);
// rs = true; boolean

Exc Example
Copy the codeThe code is as follows:

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
var regexp = /[A-E]/gi;
var rs;
while ((rs = (str)) != null)
{
    (rs);
    ();
    ("<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 the codeThe code is as follows:

var regexp = /ab*/g;
var str = "abbcdefabh";
var rs;
while ((rs = (str)) != null)
{
    (rs);
    ();
    ("<br />");
}

OUTPUT
---------------------------------
abb   3
ab     9