Javascript--exec and Match

Source: Internet
Author: User
Tags first string

Title 17:read The following JavaScript code:

var sometext= "web2.0. net2.0";
var pattern=/(\w+) (\d) \. (\d)/g;
var outcome_exec=pattern.exec (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 problem is said to be an IT company's pen test, but also cause me to write this article today, but the topic I slightly modified a bit, if you answer the question correctly, you can not look below.

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

The function of matching strings related to regular expressions in JavaScript is mainly the method of the RegExp class exec (String) and the method of the String class match (regex), and of course there are other methods that are not discussed here, But maybe a lot of programmers confuse exec and match, and here are the key features of both:

  1. Exec is a method of a regular expression, not a method of a string, and its arguments are the string, as follows:

    var re=new RegExp (/\d/);
    Re.exec ("Abc4def");

    Or use the Perl style:

    /\d/.exec ("Abc4def");

    Match is the method provided by the string class, and its arguments are regular expression objects, and the following usage is correct:

    "Abc4def". Match (\d);
  2. The exec and match returns are all arrays

    If the regular expression that executes the Exec method is not grouped (without parentheses), then if there is a match, he will return an array with only one element, the only element of which is the first string that matches the regular expression, or null if there is no match.

    The following two alert functions pop up with the same message:

    var str= "Cat,hat";
    var p=/at/; No G attribute
    Alert (p.exec (str))
    Alert (Str.match (p))

    Are all "at". exec is equivalent to match on this occasion.

    But if the regular expression is a global match (G property), then the above code results are different:

    var str= "Cat,hat";
    var p=/at/g; Note the G property
    Alert (p.exec (str))
    Alert (Str.match (p))

    respectively is
    "At"
    "At,at".

    Because exec always returns only the first match, and match returns all matches when the G attribute is specified in the regular.
  3. exec if a match is found and contains a grouping, the returned array will contain more than one element, the first element being the match found, followed by the first and second of the matching elements. Group (reverse reference)

    The following code will pop up "Cat2,at":

    var str= "Cat2,hat8";
    var p=/c (at) \d/;
    Alert (p.exec (str))

    Where the first element is the matching string "Cat2", the following element is the "at" in parentheses.
  4. The match function implements the same functionality as exec when it meets the following conditions: someone else

    1, the regular expression contains the grouping (parentheses)
    2. Returns a unique match

    And look at the following code:

    var str= "Cat2,hat8";
    var p=/c (at) \d/;
    Alert (p.exec (str))
    Alert (Str.match (p))

    Will pop up the message "Cat2,at", does it feel 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:

var sometext= "web2.0. net2.0";
var pattern=/(\w+) (\d) \. (\d)/g;
var outcome_exec=pattern.exec (Sometext);
var outcome_matc=sometext.match (pattern);

Analysis:

The value of Outcome_exec: the G attribute in pattern has no effect on the EXEC function, so exec matches the first string "web2.0" that can be matched, as the first element of its return array, and because the pattern contains three groupings ((\w+), (\d), (\d)), so the array will also contain three elements, followed by "Web", "2", "0", so the final result of the exec execution is: ["web2.0", "Web", "2", "0"]

OUTCOME_MATC value: Because pattern is globally matched, match matches all strings that can be matched, so the value of the resulting array is OUTCOME_MATC ["web2.0", "net2.0"]. If the pattern does not have a G attribute, it will be the same as the outcome_exec result, because it conforms to the condition described in section 4th of this article: there is a grouping and returns a unique match!

Summarize:

Match is an array that returns all matching string compositions, but the regular expression must specify the global G property to return all matches, and the G property will return an array of only one element.

exec always returns information related to the first match, and its return array includes the first matched string, and all the grouped reverse references.

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

In some cases, EXEC returns the same result as the match returns:

var str= "Cat,hat";
var p=/at/; No G attribute
Alert (p.exec (str))
Alert (Str.match (p))

All pop Up "at"

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

In some cases, match returns the same result as the Exec returns:

var str= "Cat2,hat8";
var p=/c (at) \d/;
Alert (p.exec (str))
Alert (Str.match (p))

All pop Up "Cat2,at"

Javascript--exec and Match

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.