Home  >  Article  >  Web Front-end  >  Learn the regular matching method of javascript_javascript skills

Learn the regular matching method of javascript_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:13:451100browse

There are three methods for regular matching in JavaScript, match, exec, and test. These methods are all related to strings and RegExp objects, but the usage scenarios are different and easy to confuse. match is a method of string, receiving a RegExp object as a parameter, and the others are methods of RegExp object, receiving a string parameter.

var str = 'abcdef12ab34cd56ef';
var patt = new RegExp('ab'); //主意是非全局匹配

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. If a string matching regExp is found in the string, it will return true. If no matching string is found, it will return false

2. regExp.exec(string)

This method is slightly more complicated.

When regExp has no global flag, its return value is a string array: the 0th element of the array is the string just matched. If regExp has a subexpression, the 1st element of the array is the first element of regExp. Subexpression, the second element 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 a string array: ['f12','1', '2'].

When regExp has the global flag (g option), the return value is an array composed of the first matched string. The 0th element of the array is the string just matched. If regExp has a subexpression, Then the first element of the array is the first subexpression of regExp, the second element of the array is the second subexpression of regExp... and so on. At the same time, an attribute (lastIndex) of the regExp object is changed, and lastIndex is set to the position of the last character of the string, the position after it (in the above example, lastIndex = 2). When regExp.exec(string) is called again, the search range will be searched starting from regExp.lastIndex. At this time, the return value is still a single-element string array, lastIndex = 10. We often use a while loop to iterate over matches in a string:

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

What the exec method returns is not a standard array, but should be regarded as a class array, because it also has two attributes: input is the input string, and index is the position of the first character of the currently matched string in input.

3. string.match(regExp)

This method is simpler than exec because it does not need to consider the lastIndex attribute of regExp. Similarly, two situations need to be distinguished (global matching and non-global matching)

When regExp has no global flag, the return value is the same as calling exec, returning an array. The 0th element of the array is the string just matched. If regExp has a subexpression, the 1st element of the array is regExp. The first subexpression of , the second element is the second subexpression of regExp...and so on. The idea is that the array also has two attributes: input is the input string string, and index is the position of the first character of the currently matched string in input.

When regExp has the global flag (g option), it is very simple and consistent with our understanding: return an array composed of all matched strings. This is a standard array with no input attributes and no index attributes. There is no other information in the return value array except the matched string.

From the above analysis, if you just want to determine whether a string matches a certain regular expression, use the test method. If you want to retrieve all matching strings at once, or just find the first matching string, use the match method. If you want to match multiple times and need to know the position of each matched string in the original string, or there is subexpression information in the regular expression that you need to pay attention to, use the exec method.

The above is an introduction to various methods of regular matching in JavaScript. I hope it will be helpful to everyone's learning.

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