A deep understanding of the similarities and differences between exec and match methods in javascript

Source: Internet
Author: User
Tags first string

A deep understanding of the similarities and differences between exec and match methods in javascript

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 an IT company and also the reason why I wrote this article today. However, I have slightly modified the question. If you have answered the question correctly, you can skip the following steps.

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

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, we will not discuss it here, but many programmers may confuse exec and match. 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:

    Var re = 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:

    Var str = "cat, hat ";
    Var p =/at/; // 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 a global match (g attribute), the above Code results are different:
    Var str = "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, ":
    Var str = "cat2, hat8 ";
    Var p =/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:
    Var str = "cat2, hat8 ";
    Var p =/c (at) \ d /;
    Alert(p.exe c (str ))
    Alert (str. match (p ))

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

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

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

    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:
    Var str = "cat, hat ";
    Var p =/at/; // 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:
    Var str = "cat2, hat8 ";
    Var p =/c (at) \ d /;
    Alert(p.exe c (str ))
    Alert (str. match (p ))

    "Cat2, at" is displayed"

    Json2form, an industry-renowned form development framework, greatly improves the efficiency of your development of information systems. View demo

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.