JavaScript's regular matching method learning _javascript skills

Source: Internet
Author: User

JavaScript is matching 3 methods, Match,exec,test. These methods are related to strings and RegExp objects, but the use of scenes is not the same, easy to confuse. Match is a method of a string that receives a RegExp object as a parameter, and the other is a method of the RegExp object that receives a string parameter.

var str = ' Abcdef12ab34cd56ef ';
var Patt = new RegExp (' AB '); Idea is a non 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)

The simplest method is to return True if a string is found to match regexp, false if no matching string is found

2. Regexp.exec (String)

The method is slightly more complicated.

When RegExp does not have a global flag, its return value is an array of strings: the No. 0 element of the array is the string that is just matched, and if RegExp has a subexpression, the array 1th element is the first subexpression of RegExp, and the second word of the regexp with the 2nd element ... Analogy In the example above, 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 an array of the first matched strings, the No. 0 element of the array is the string that is just matched, and if RegExp has a subexpression, the array 1th element is the first subexpression of RegExp. The 2nd element is the second word expression of regexp ... Analogy At the same time, an attribute of the RegExp object (lastindex) is changed, Lastindex is set to the position of the last character of the string, and the position behind it (in the example above is lastindex = 2). When Regexp.exec (string) is called again, the search scope starts searching from Regexp.lastindex. The return value at this time is still an array of cell elements, lastindex = 10. We often use a while loop to traverse a match in a string:

var Patt = new RegExp (' Ab ', ' g '),
str = ' abcdef12ab34cd56ef ', ret;
while (ret = patt.exec (str))!=null) {
  console.log (ret);
}
Output
[' AB ']
[' AB ']

The Exec method returns not a standard array, which should be considered an array of classes, because it has 2 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 take into account the RegExp lastindex attribute. Similarly, there are two situations that need to be divided (global and non-global matches)

When RegExp does not have a global flag, the return value is the same as calling exec, which returns an array with the No. 0 element of the array as just the string, and if RegExp has a subexpression, the array 1th element is the first subexpression of the RegExp. The 2nd element is the second word expression of regexp ... Analogy Idea the array also has 2 properties: input is the string string,index is the current matching string where the first character is located in the input.

When RegExp has a global flag (g option), it's very simple, and it's also consistent with our understanding: Returns an array of all the matching strings. This is a standard array with no input attribute and no Index property. The return value array has no other information than the string that matches it.

As you can see from the above analysis, if you just want to determine whether a string matches a regular expression, use the test method. Use the match method if you want to remove all matching strings at once, or just find the first matching string. If you want to match multiple times, and you need to know where each matched string is located in the original string, or if there are subexpression information in the regular expression that needs attention, use the Exec method.

The above is JavaScript is the matching of a variety of methods to introduce, I hope to help you learn.

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.