The difference between Test, EXEC, MATCH method in JS regular expressions
test
Test returns Boolean to find the existence mode 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 results, and returns in the form of an array.
var str = "1A1B1C";
var reg = new regexp ("1.", "");
var anrr = reg.exec (str);
If there is no mode, ARR is null, otherwise ARR is always an array of 1 in length, and its value is the current matching. There are three attributes of ARR: the position of the current matching of the INDEX; the position of LastIndex's current matching item (the length of the INDEX + current matching); input is the STR in the example above.
EXEC method is affected by parameter G. If G is specified, when EXEC is called next time, you will start from LastIndex, which was matched.
var str = "1A1B1C";
var reg = new regexp ("1.", "");
alert (reg.exec (str) [0]);
alert (reg.exec (str) [0]);
The two outputs above are 1A. Now look 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 1A, the second output 1B.
match
Match is a method of String object.
var str = "1A1B1C";
var reg = new regexp ("1.", "");
alert (str.match (reg));
This method is a bit like EXEC, but: EXEC is the 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 the parameter G is specified, then the match will return to all the results at a time.
var str = "1A1B1C";
var reg = new regexp ("1.", "g");
alert (str.match (reg));
//alerts (Str.match (Reg)); // This sentence is the same as the results of the above sentence are the same as above.
This result is an array with three elements, which are: 1A, 1B, 1C.
Two functions are often used in JavaScript, and Match and Test are often used in regular expressions. Of course, there are exec. Here is a code example to distinguish the difference between them.
Match exmple
Copy code code as follows:
var str = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
var regexp = /[ae] /gi;
var rs = str.match (regexp);
// rs = array ('a', 'b', 'c', 'd', 'e', 'a', 'b', 'c', 'd', 'e');
Test Example
Copy code code as follows:
var str = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
var regexp = /[ae] /gi;
var rs = regexp.test (STR);
// rs = true; BOOLEAN
Exc EXAMPLE
Copy code code as follows:
var str = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
var regexp = /[ae] /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 exmple
Copy code code 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