The difference explanation of Test,exec,match method in JS regular expression _javascript skill

Source: Internet
Author: User

The difference explanation of Test,exec,match method in JS regular expression

Test
Test returns a Boolean to find out if 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 as an array.
var str = "1a1b1c";
var reg = new RegExp ("1.", "");
var arr = reg.exec (str);
If the pattern does not exist, then 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 for index, lastindex the position at which the current match ends (index + the length of the current match); input is str in the example above.


The Exec method is affected by the parameter G. If G is specified, the next time you call Exec, the lookup starts with 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 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 is 1a and the second output is 1b.


Match
Match is a method of a String object.
var str = "1a1b1c";
var reg = new RegExp ("1.", "");
Alert (Str.match (reg));
Match is a bit like exec, but: exec is the way to REGEXP objects; Math is a method of String objects. A different point is the explanation of the parameter G.
If 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)); The result of this sentence is the same
The result is an array of three elements: 1a, 1b, 1c.

Regular expressions are often used in JavaScript, while regular expressions often use the two functions of match and test, and of course exec. Here's a code example to differentiate between them.

Match Example

Copy Code code 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 Code code as follows:

var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
var regexp =/[a-e]/gi;
var rs = regexp.test (str);
rs = true; Boolean

exc Example
Copy Code code 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 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.