An in-depth analysis of Exec and Match methods in JavaScript _javascript skills

Source: Internet
Author: User
Tags first string

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

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

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

Both exec and match return arrays.

If a regular expression executing the Exec method is not grouped (without parentheses), if there is a match, he returns an array of 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 information:

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

Are "at". exec is equivalent to match in this situation.

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

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

respectively is

"At"

"At,at".

Because exec always returns only the first match, the match returns all matches when the G attribute is specified.

exec if a match is found, and the grouping is included, the returned array will contain multiple elements, the first element is the match found, and the following element is the first, second in the match ... Grouping (reverse reference)

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

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

The first element is the matching string "Cat2", and the following element is the matching "at" in parentheses.

The match function will take over when it satisfies the following conditions, and realize the same functions as exec:

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", is that strange?

Summarize:

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

EXEC always returns the information associated with the first match, and its return array includes the first matching string, and all the backward references for the group.

In some cases the results returned by EXEC are the same as the results returned by match:

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 exec returns:

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

pops up "Cat2,at."

Above this in-depth analysis of JavaScript in the Exec and match method is small to share all the content of everyone, hope to give you a reference, but also hope that we support the cloud habitat community.

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.