Javascript Regular Expression matching method learning _ javascript skills

Source: Internet
Author: User
This article mainly introduces the regular expression matching method of javascript to help you learn the related content of javascript regular expressions more quickly and efficiently, interested friends can refer to three matching methods in javascript: match, exec, and test. These methods are related to strings and RegExp objects, but they are easy to be confused due to different use cases. Match is a string method that receives a RegExp object as a parameter. Others are RegExp object methods that receive a string parameter.

Var str = 'abcdef12ab34cd56ef '; var patt = new RegExp (' AB '); // The idea is 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.exe c (str); console. log (ret_exec );

1. regExp. test (string)

This method is the simplest. If a string matches regExp is found in string, true is returned. If no matching string is found, false is returned.

2. regExp.exe c (string)

This method is a little more complex.

When regExp does not have a global flag, the returned value is a string array: Element 0th of the array is the matching string. If regExp has a subexpression, the array element 1st is the first subexpression of regExp, And the element 2nd is the second word expression of regExp... and so on. In the above example, if patt = new RegExp ('f (\ d) ', 'G'); then ret_exec will be a string array: ['f12 ', '1', '2'].

When regExp has a global sign (g option), the returned value is an array consisting of the first matched string. Element 0th of the array is the matching string. If regExp has a subexpression, the array element 1st is the first subexpression of regExp, And the element 2nd is the second word expression of regExp... and so on. At the same time, an attribute (lastIndex) of the regExp object was changed, and lastIndex was set to the position of the last character of the string, the location next to it (in the above example, lastIndex = 2 ). When regExp.exe c (string) is called again, the search range starts from regExp. lastIndex. The returned value is still a single-element string array with lastIndex = 10. We often use the while loop to traverse the matching in the string:

Var patt = new RegExp ('AB', 'G'), str = 'abcdef12ab34cd56ef ', ret; while (ret = patt.exe c (str ))! = Null) {console. log (ret);} // output ['AB'] ['AB']

The exec method does not return a standard array. It should be considered a class array because it has two attributes: input is the input string, 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. Likewise, two cases are required (Global match and non-global match)

When regExp does not have a global flag, the return value is the same as the exec call. An array is returned. Element 0th of the array is the matched string. If regExp has a subexpression, the array element 1st is the first subexpression of regExp, And the element 2nd is the second word expression of regExp... and so on. The idea is that the array also has two attributes: input is the input string, and index is the position of the first character of the currently matched string in the input.

When regExp has a global sign (g option), it is very simple. In our understanding, it returns an array consisting of all matched strings. This is a standard array with no input or index attributes. The returned value array contains no information except the matched strings.

From the analysis above, if you just want to determine whether the string matches a regular expression, use the test method. If you want to retrieve all matched strings at a time, or find only the first matched string, you can use the match method. If you want to match the string multiple times, and you need to know the position of each matched string in the original string, or the regular expression contains sub-expression information that needs to be concerned, use the exec method.

The above is an introduction to various methods of javascript Regular Expression matching, and I hope it will be helpful for your learning.

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.