The difference between String.match () and Regexp.exec () in JavaScript _javascript tips

Source: Internet
Author: User
1. These two methods, if the match succeeds, returns an array, the match fails, and returns NULL.
2. When the global property of the RegExp is false, the return array of the two methods is the same.

The No. 0 element of the array is the first matching string for the entire pattern, and the next element is the substring of the first match in pattern.
In addition, the array also has index and input two additional attributes, index is the starting position of the matching string, input is the entire input string.
At this point, the Lastindex property of RegExp is always 0.
Demo
Copy Code code as follows:

var s = ' This is a string ';
var p =/\b\w* (i) s\b/;
var rm = S.match (p);
var re = p.exec (s);
Console.log (' Match_array: ' + json.stringify (RM));
Console.log (' Match_array_index: ' + rm.index);
Console.log (' match_array_input: ' + rm.input);
Console.log ('----------------------------');
Console.log (' Exec_array: ' + json.stringify (re));
Console.log (' Exec_array_index: ' + re.index);
Console.log (' exec_array_input: ' + re.input);

Display as result (Firefox console):
Copy Code code as follows:

Match_array: ["This", "I"]
match_array_index:0
Match_array_input:this is a string
----------------------------
Exec_array: ["This", "I"]
exec_array_index:0
Exec_array_input:this is a string

3. When the global property of the RegExp is true, the returned array is different.
The match method returns an array that contains all the matching strings, no child matching strings, and extra attributes. At this point, the Lastindex property is invalid.
The Exec method returns the same array format as when the global is false, except that the RegExp lastindex property is valid at this point, and the match starts with the character indicated by the lastindex. And the method executes lastindex to the next character of the matching string, so the Exec method loops through the entire string, until the string returns null and the lastindex is placed 0.
Demo
Copy Code code as follows:

var s = ' This is a string ';
var p =/\b\w* (i) s\b/g;
var rm = S.match (p);
var re;
Console.log (' Match_array: ' + json.stringify (RM));
Console.log (' Match_array_index: ' + rm.index);
Console.log (' match_array_input: ' + rm.input);
while (re = p.exec (s)) {
Console.log ('----------------------------');
Console.log (' Exec_array: ' + json.stringify (re));
Console.log (' Exec_array_index: ' + re.index);
Console.log (' exec_array_input: ' + re.input);
Console.log (' Regexp_lastindex: ' + p.lastindex);
}
Console.log ('----------------------------');
Console.log (' Exec_array: ' + re);
Console.log (' Regexp_lastindex: ' + p.lastindex);

Results:
Copy Code code as follows:

Match_array: [' This ', ' is ']
match_array_index:undefined
match_array_input:undefined
----------------------------
Exec_array: ["This", "I"]
exec_array_index:0
Exec_array_input:this is a string
Regexp_lastindex:4
----------------------------
Exec_array: ["is", "I"]
Exec_array_index:5
Exec_array_input:this is a string
Regexp_lastindex:7
----------------------------
Exec_array:null
regexp_lastindex:0

Comprehensive:

1. The match and Exec methods have the same effect when there is no G identifier, and the Exec method can provide the most complete matching result when there is a G identifier.
2. By the way, here's a regexp.test () method, which is a simplified version of the Exec method, returns True if there is a match, returns false without a match, and executes the same procedure as exec. Equivalent to (P.exec (s)!= null).
The Lastindex property of 3.RegExp has a G identifier and is valid in the Exec and test methods and is not available elsewhere.
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.