Differences between exec and match in js Regular Expressions _ javascript skills

Source: Internet
Author: User
This article mainly introduces the differences between exec and match expressions in js regular expressions. If you need them, you can refer to them and hope to help you. In the past, js regular expressions were rarely used in JavaScript, that is, it is used to determine the Mail name. There are a lot of online code and there is little research.

Recently, we have encountered some issues that require regular expressions.

The regular expression object has two definitions ::

1. Definition 1:

New RegExp (pattern, attributes); for example, var reg = new RegExp ("abc", "g ")

Here, pattern indicates the expression content, and the above indicates matching abc

Attributes: g, global match, I is case insensitive, m executes multi-row match, use the most g and I

2. Definition 2:/pattern/attributes.

For example, var reg =/abc/g;

Rules of regular expression are not described here. Only the difference between exec and match is recorded:

1. exec is a regular expression method, not a string method. Its parameter is a string, as shown below:

As defined above
Var reg = new RegExp ("abc ");
Var str = "3abc4, 5abc6 ";
Reg.exe c (str );

2. match is a string method for matching regular expression rules. Its parameters are regular expressions, as shown in figure

Var reg = new RegExp ("abc ");
Var str = "3abc4, 5abc6 ";
Str. match (reg );

3. exec and match return arrays;

If the regular expression executed by exec does not have a subexpression (the content in parentheses, such as (\ s *) in/abc (\ s *)/, if there is a match, returns the content of the first matched string. At this time, the array has only one element. If there is no matching, null is returned;

Var reg = new RegExp ("abc ");
Var str = "3abc4, 5abc6 ";
Alert(reg.exe c (str ));
Alert (str. match (reg ));

After executing the code above, you will find that the content of both is the same: abc,

4. If the regular expression object is defined as a global match, for example:

Var reg = new RegExp ("abc", "g ");
Var str = "3abc4, 5abc6 ";
Alert(reg.exe c (str ));
Alert (str. match (reg ));

It is abc, abc, and abc. Because match performs a global match query, exec returns only one matching if no subexpression exists.

5. When the expression contains a subexpression:

Var reg = new RegExp ("a (bc )");
Var str = "3abc4, 5abc6 ";
Alert(reg.exe c (str ));
Alert (str. match (reg ));

You will find that the execution results of both are: abc, bc;

6. If the regular expression object is defined as a global match

Var reg = new RegExp ("a (bc)", "g ");
Var str = "3abc4, 5abc6 ";
Alert(reg.exe c (str ));
Alert (str. match (reg ));

The returned results are abc, bc, abc, abc,

Summary:

1. When the regular expression has no subexpression and is defined as a non-global match, the results of exec and match execution are the same, and the first matching string content is returned;

2. When the regular expression has no subexpression and is defined as a global match, exec and match are executed and there are multiple matching contents, match returns multiple element arrays;

3. When a regular expression has a subexpression and is defined as a non-global match, the results of exec and match execution are the same as those in the above 5th cases;

4. When a regular expression has a subexpression and is defined as a global match, the results of exec and match operations are different. In this case, match ignores the subexpression, only search for the full match Regular Expression and return all content, for example, in the above 6th cases;

That is to say, exec has no relation with whether the global is defined, while match is associated globally. If it is defined as non-global, the execution results of the two are the same.

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.