Differences between string.match()and regexp.exe c () in javascript

Source: Internet
Author: User

1. If the two methods match successfully, an array is returned. If the match fails, null is returned.
2. When the global attribute of RegExp is false, the returned arrays of the two methods are the same.

The 0th elements in the array are the first matching string of the entire pattern, and the next element is the child matching string in the First Matching of pattern.
In addition, the array has two additional attributes: index and input. index is the starting position of the matching string, and input is the entire input string.
At this time, the lastIndex attribute of RegExp is always 0.
Demo:Copy codeThe Code is as follows: var s = 'this is a string ';
Var p =/\ B \ w * (I) s \ B /;
Var rm = s. match (p );
Var re = p.exe c (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 );

The result is displayed on the firefox console ):Copy codeThe Code is 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 attribute of RegExp is true, the returned array is different.
The array returned by the match method contains all matching strings, without any child matching strings or additional attributes. The lastIndex attribute is invalid.
The array format returned by the exec method is the same as that returned when the global value is false, but the lastIndex attribute of RegExp is valid at this time. The matching starts from the character indicated by lastIndex, after the method is executed, lastIndex is set to the next character of the matched string. Therefore, when the exec method is executed cyclically, the entire string is matched in sequence until the string returns null, set lastIndex to 0.
Demo:Copy codeThe Code is 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.exe c (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 );

Result:Copy codeThe Code is 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

To sum up:

1. When there is no g identifier, the effect of the match and exec methods is the same; when there is a g identifier, the exec method can provide the most complete matching results.
2. here, by the way, we will mention RegExp. the test () method is a simplified version of the exec method. If a matching result is returned, true is returned. If no matching result is returned, false is returned. The execution process is the same as exec. Equivalent to (p.exe c (s )! = Null ).
3. The lastIndex attribute of RegExp has a g identifier and is valid in the exec and test methods. Otherwise, it is invalid.

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.