Differences between the test, exec, and match methods in js Regular Expressions _ javascript skills

Source: Internet
Author: User
This article mainly introduces the differences between the test, exec, and match methods in js regular expressions. If you need them, you can refer to them and hope to help you. Differences between the test, exec, and match methods in js Regular Expressions

Test
Test returns a Boolean value to check whether the corresponding string contains a pattern.
Var str = "1a1b1c ";
Var reg = new RegExp ("1 .","");
Alert (reg. test (str); // true


Exec
Exec searches for and returns the current matching result, and returns the result as an array.
Var str = "1a1b1c ";
Var reg = new RegExp ("1 .","");
Var arr = reg.exe c (str );
If the mode does not exist, arr is null. Otherwise, arr is always an array with a length of 1, and its value is the current match. Arr also has three attributes: the position of the current matching item of the index; the position of the end of the current matching item of the lastIndex (index + Length of the current matching item); input is str in the preceding example.


The exec method is affected by the parameter g. If g is specified, the next time exec is called, it will start from the last matched lastIndex.
Var str = "1a1b1c ";
Var reg = new RegExp ("1 .","");
Alert(reg.exe c (str) [0]);
Alert(reg.exe c (str) [0]);
Both outputs are 1a. Now let's look at the specified parameter g:
Var str = "1a1b1c ";
Var reg = new RegExp ("1.", "g ");
Alert(reg.exe c (str) [0]);
Alert(reg.exe c (str) [0]);
The first output is 1a and the second output is 1b.


Match
Match is a method of String objects.
Var str = "1a1b1c ";
Var reg = new RegExp ("1 .","");
Alert (str. match (reg ));
Match is a bit like exec, but exec is the RegExp object method; math is the String object method. There is another difference between the two, that is, the interpretation of the parameter g.
If the parameter g is specified, match returns all results at a time.
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 as that in the same sentence.
The result is an array with three elements: 1a, 1b, and 1c.

Regular Expressions are often used in JavaScript, while regular expressions often use the Match and Test functions. Of course, Exec is also used. Here we use code instances to differentiate the differences between them.

Match Example

The Code is as follows:


Var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
Var regexp =/[A-E]/gi;
Var rs = str. match (regexp );
// Rs = Array ('A', 'B', 'C', 'D', 'E', 'E', 'A', 'B', 'C ', 'D', 'E ');


Test Example

The Code is as follows:


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


Exc Example

The Code is as follows:


Var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ";
Var regexp =/[A-E]/gi;
Var rs;
While (rs = regexp.exe c (str ))! = Null)
{
Document. write (rs );
Document. write (regexp. lastIndex );
Document. write ("
");
}

OUTPUT
---------------------------------
A 1
B 2
C 3
D 4
E 5
A 27
B 28
C 29
D 30
E 31


Another Exc Example

The Code is as follows:


Var regexp =/AB */g;
Var str = "abbcdefabh ";
Var rs;
While (rs = regexp.exe c (str ))! = Null)
{
Document. write (rs );
Document. write (regexp. lastIndex );
Document. write ("
");
}

OUTPUT
---------------------------------
Abb 3
AB 9

Related Article

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.