Regular matching method of javascript

  • 2021-01-02 21:45:28
  • OfStack

There are three regular matching methods in javascript, match,exec and test. These methods are all related to strings and RegExp objects, but they are not used in the same way and can be confusing. match is a method of a string that takes 1 RegExp object as an argument, and the other methods of an RegExp object take 1 string argument.


var str = 'abcdef12ab34cd56ef';
var patt = new RegExp('ab'); // The idea is not a global match 

var ret_test = patt.test(str);
console.log(ret_test);
var ret_match = str.match(patt);
console.log(ret_match);
var ret_exec = patt.exec(str);
console.log(ret_exec);

1. regExp.test(string)

This method is the simplest, returning true if a string matching regExp is found in string and false if no match is found

2. regExp.exec(string)

This method is a little more complicated.

When regExp does not have a global flag, its return value is an array of strings: element 0 of the array is the string just matched, if regExp has a subexpression, element 1 of the array is the first subexpression of regExp, element 2 is the second subexpression of regExp... And so on. In the above example, if patt = new RegExp('f(\\d)(\\d)','g'); Then ret_exec will be an array of strings: ['f12','1','2'].

When regExp has a global flag (g option), the return value is the array of the first matched string, the 0th element of the array is the string just matched, if regExp has a subexpression, the first element of the array is the first subexpression of regExp, the second element of regExp is the second word expression... And so on. Also, the idea is that the 1 property of the regExp object (lastIndex) is changed and lastIndex is set to the last character of that string, followed by the last character (lastIndex = 2 in the above example). When regExp.exec (string) is called again, the search range starts at regExp.lastIndex. The return value is still an array of single-element strings, lastIndex = 10. We often use the while loop to iterate over matches in strings:


var patt = new RegExp('ab', 'g'),
  str = 'abcdef12ab34cd56ef', ret;
while((ret = patt.exec(str))!=null) {
  console.log(ret);
}
// The output 
['ab']
['ab']

The exec method returns a class array instead of a standard array, because it also has two properties: input is the input string, and index is the position of the first character of the current matching string in input.

3. string.match(regExp)

This method is 1 simpler than exec because it does not take into account the lastIndex attribute of regExp. Again, there are two cases (global and non-global)

When regExp does not have a global flag, the return value is the same as the call to exec1, returning 1 array, the 0th element of the array is the string just matched, if regExp has a subexpression, the first element of the array is the first subexpression of regExp, the second element of regExp... And so on. The array also has two properties: input is the input string string, and index is the position of the first character of the current matching string in input.

When regExp has a global flag (the g option), it's simple and as we understand it: return an array of all matched strings. This is a standard array, with no input attribute, and no index attribute. There is no information in the return value array other than the string that was matched.

From the above analysis, if you just want to determine if a string matches a regular expression, use the test method. If you want to extract all the matched strings first time, or just find the first string that matches, use the match method. If you want multiple matches and you need to know where each string is in the original string, or if you have subexpression information in the regular expression that needs attention, use the exec method.

The above is the introduction of javascript regular matching methods, I hope to help you learn.


Related articles: