Home >Web Front-end >JS Tutorial >Explanation of the difference between String.match() and RegExp.exec() in javascript_javascript skills

Explanation of the difference between String.match() and RegExp.exec() in javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:44:05981browse

1. These two methods, if the match is successful, return an array, if the match fails, return null.
2. When the global attribute of RegExp is false, the return arrays of these two methods are the same.

The 0th element of the array is the first matching string of the entire pattern, and the following elements are the submatching strings of the first matching of the 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 code The code is as follows:

var s = 'this is a string';
var p = /bw*(i)sb/;
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);

The display result is (firefox control Taiwan):
Copy code The 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 submatching strings and additional attributes. At this time, the lastIndex attribute is invalid.
The array format returned by the exec method is the same as when global is false, except that the lastIndex attribute of RegExp is valid at this time. The matching starts from the character indicated by lastIndex, and after the method is executed, lastIndex will be set to the current matching string. at the next character, so when the exec method is executed in a loop, the entire string will be matched in sequence until the string finally returns null, and lastIndex is set to 0.
demo:
Copy code The code is as follows:

var s = 'this is a string';
var p = /bw*(i)sb/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);

Result:
Copy code The 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

In summary:

1. When there is no g identifier, the match and exec methods have the same effect; when there is the g identifier, the exec method can provide the most complete matching results.
2. By the way, the RegExp.test() method is mentioned here. It is a simplified version of the exec method. If there is a matching result, it returns true. If there is no matching result, it returns false. The execution process is the same as exec. Equivalent to (p.exec(s) != null).
3. The lastIndex attribute of RegExp has the g identifier and is valid in the exec and test methods, but is invalid elsewhere.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn