Global matching pattern Analysis _javascript techniques for JavaScript regularization expressions

Source: Internet
Author: User
Copy Code code as follows:

var str = "123#ABC";
var re =/abc/ig;
Console.log (Re.test (str)); Output ture
Console.log (Re.test (str)); Output false
Console.log (Re.test (str)); Output ture
Console.log (Re.test (str)); Output false

If you use the "G" identifier when you create a regular expression object, or if you set its Global property value to Ture, the newly created regular expression object will use the pattern to match the string that you want to match globally. In global match mode, you can perform multiple matches on the string that you specify to find. Each match uses the value of the Lastindex property of the current regular object as the starting position to start the lookup in the target string. The initial value of the Lastindex property is 0, and after the matching item is found, the value of lastindex is reset to the position index of the next character in the string that matches the content, which identifies where to start the next time the match is performed. If no matching item is found, the value of lastindex is set to 0. When no global match flag for a regular object is set, the value of the Lastindex property is always 0, and each execution match finds only the first matching item in the string. The following code can be used to view the values that match the corresponding lastindex property in the execution.
Copy Code code as follows:

var str = "123#ABC";
var re =/abc/ig;
Console.log (Re.test (str)); Output ture
Console.log (Re.lastindex); Output 7
Console.log (Re.test (str)); Output false
Console.log (Re.lastindex); Output 0
Console.log (Re.test (str)); Output ture
Console.log (Re.lastindex); Output 7
Console.log (Re.test (str)); Output false
Console.log (Re.lastindex); Output 0

About the RegExp.prototype.exec (str) method and the String.prototype.math (rgexp) method

The test method return value of the regular object is true or flase, which is useful when you need to detect only that the target string matches the specified pattern, but does not need to get the matching content. You need to use the RegExp type exec (str) method or string match (Rgexp) method when you need to obtain a matching result.

The RegExp.prototype.exec (str) method returns a null or a return array in which the No. 0 element of the array holds the matching content found in string str, and 1 to n elements return the contents of the child matches specified in the pattern using parentheses "()".

The String.prototype.math (rgexp) method and RegExp.prototype.exec (str) behavior are similar when the global flag is not used. The array item element 0 to n that is returned by the String.prototype.math (Rgexp) method when the global match flag is set contains all the matching items that do not contain child matches. You can then use regexp.$1..$9 to get 9 child matches.

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.