Js regular expression test exec match method distinction

  • 2020-03-30 01:29:36
  • OfStack

Js regular expression test, exec, match method distinction

The test
Test returns a Boolean to find out if the pattern exists in the corresponding string.
Var STR = "1 a1b1c";
Var reg = new RegExp("1.", "");
Alert (reg. Test (STR)); / / true


The exec
Exec finds and returns the current match as an array.
Var STR = "1 a1b1c";
Var reg = new RegExp("1.", "");
Var arr = reg. The exec (STR);
If there is no pattern, 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 current match at index; The end position of the current match of lastIndex (index + the length of the current match); Input in the example above, the input is STR.


The exec method is affected by the parameter g. If g is specified, the next time exec is called, the search starts with the lastIndex that matched.
Var STR = "1 a1b1c";
Var reg = new RegExp("1.", "");
Alert (reg. The exec (STR) [0]).
Alert (reg. The exec (STR) [0]).
Both outputs are 1a. Now look at the specified parameter g:
Var STR = "1 a1b1c";
Var reg = new RegExp("1.", "g");
Alert (reg. The exec (STR) [0]).
Alert (reg. The exec (STR) [0]).
Above the first output 1a, the second output 1b.


The match
A match is a method of a String object.
Var STR = "1 a1b1c";
Var reg = new RegExp("1.", "");
Alert (STR) match (reg));
Match this method is a bit like exec, except that exec is a method of RegExp objects; Math is a method of a String object. Another difference between the two is the interpretation of the parameter g.
If the parameter g is specified, then match returns all the results at once.
Var STR = "1 a1b1c";
Var reg = new RegExp("1.", "g");
Alert (STR) match (reg));
/ / alert (STR) match (reg)); // the result of this sentence is the same as that of the preceding one
The result is an array with three elements: 1a, 1b, and 1c.

Regular expressions are used a lot in JavaScript, and the two functions Match and Test are used a lot in regular expressions, as well as Exec.

Match Example


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');

The Test Example

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

Exc Example

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

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


Related articles: