Thoroughly understand the exec and match methods in Javascript

Source: Internet
Author: User

Before reading this article, read the following question:

Question 17: Read the following JavaScript code:

VaR sometext = "Web2.0. net2.0 ";
VaR pattern =/(\ W +) (\ D) \. (\ D)/g;
VaR outcome_exec=pattern.exe C (sometext );
VaR outcome_matc = sometext. Match (pattern );

What is outcome_exec [1] and outcome_matc [1]?

Choice A: True
Choice B: false
Choice C: NULL
Choice D: Web
Choice E: Web2.0
Choice F: Undefined
Choice G: net2.0

This question is said to be a pen question from Sangfor Corporation and also caused me to write this article today.ArticleBut I modified the question a little. If you are correct, you may not read it below.

-----------------------------------------------------------------

Functions related to regular expressions in Javascript that match strings mainly include the Regexp class method exec (string) and the string class method match (RegEx). Of course, there are some other methods, I will not discuss it here, but there may be manyProgramBoth exec and match are obfuscated. Here we will list the key features of the two:

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

    VaRRe = new Regexp (/\ D /);
    Re.exe C ("Abc4def");

    Or use the Perl style:

    /\ D/. Exec ("Abc4def");

    Match is the method provided by the string class. Its parameter is a regular expression object. The following usage is correct:

    "Abc4def". Match (\ D );
     

  2. Both exec and match return arrays.

    If the regular expression that executes the exec method is not grouped (the content is not enclosed in parentheses), an array with only one element will be returned if there is a match, the unique element of this array is the first string that the regular expression matches. If there is no match, null is returned.

    The information displayed by the following two alert functions is the same:

    VaRSTR ="Cat, hat";
    VaR P =//;//No G attribute
    Alert(p.exe C (STR ))
    Alert (Str. Match (p ))

    All are "". In this case, exec is equivalent to match.

    However, if the regular expression is globally matched (G attribute ),CodeThe results are different:

    VaRSTR ="Cat, hat";
    VaR P =/AT/g;// Note the G attribute
    Alert(p.exe C (STR ))
    Alert (Str. Match (p ))

    They are
    ""
    "At, ".

    Because exec always returns only the first match, and when the G attribute is specified in the regular expression, all matches are returned.
     

  3. If exec finds a match and contains a group, the returned array will contain multiple elements. The first element is the matching, the following elements are the first and second elements in the match... groups (reverse reference)

    The following code will pop up "cat2, ":

    VaRSTR ="Cat2, hat8";
    VaRP =/C (AT) \ D /;
    Alert(p.exe C (STR ))

    The first element is the matching string "cat2", and the subsequent element is the matching "at" in the brackets ".
     

  4. The match function performs the same functions as exec when the following conditions are met:

    1. Regular Expressions contain grouping (parentheses)
    2. Return a unique match.

    See the following code:

    VaRSTR ="Cat2, hat8";
    VaRP =/C (AT) \ D /;
    Alert(p.exe C (STR ))
    Alert (Str. Match (p ))

    The message "cat2, at" is displayed. Is it strange?

The following links provide some demos: http://www.webchat.com.cn/exec_match.htm

Now let's review the questions raised at the beginning of the article:

VaRSometext ="Web2.0. net2.0";
VaRPattern =/(\ W +) (\ D) \. (\ D)/g;
VaROutcome_exec=pattern.exe C (sometext );
VaROutcome_matc = sometext. Match (pattern );

Analysis:

Outcome_exec value: the G attribute in pattern does not have any effect on the exec function. Therefore, exec matches the first matching string "Web2.0" as the first element in the returned array, in addition, because pattern contains three groups (\ W +), (\ d), and (\ D), the array also contains three elements, followed by "Web", "2", and "0". Therefore, the final result after exec execution is: ["Web2.0", "Web", "2 ", "0"]

Outcome_matc value: Because pattern is globally matched, match matches all strings that can be matched. Therefore, the outcome_matc value of the result array is ["Web2.0", "net2.0"]. If pattern does not have the G attribute, it will be the same as the outcome_exec result, because it meets the condition described in section 4th: there are groups and a unique match is returned!

Summary:

Match is an array composed of all matching strings. However, a regular expression must specify the global G attribute to return all matching strings. If the G attribute is not specified, an array with only one element is returned.

Exec always returns information related to the first match. The returned array includes the first matched string, and the reverse reference of all groups.

-------------------------------------------

In some cases, the results returned by exec are the same as those returned by match:

VaRSTR ="Cat, hat";
VaR P =//;//No G attribute
Alert(p.exe C (STR ))
Alert (Str. Match (p ))

"At" is displayed"

-------------------------------------------

In some cases, the results returned by match are the same as those returned by Exec:

VaRSTR ="Cat2, hat8";
VaRP =/C (AT) \ D /;
Alert(p.exe C (STR ))
Alert (Str. Match (p ))

"Cat2, at" is displayed"

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.