Let's take a look at a JavaScript question. It is said that it is a JavaScript question for a well-known Internet company in China. If you do not know about the global matching mode of regular expressions, you may be confused about the following output results.
The Code is 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
When a regular expression object is created with a "g" identifier or its unique global attribute value is set to true, then, the newly created Regular Expression object uses the pattern to perform global match on the string to be matched. In global match mode, you can perform multiple matches on the specified string to be searched. Each match uses the value of the lastIndex attribute of the current regular object as the starting position for starting search in the target string. The initial value of the lastIndex attribute is 0. After a matched item is found, the value of lastIndex is reset to the position index of the next character in the matching content, which is used to identify the position where the search starts when the next matching is executed. If the value of lastIndex cannot be found, it is set to 0. When the global match flag of the regular object is not set, the value of the lastIndex attribute is always 0. Only the first matching item in the string is searched for each matching operation. You can use the following code to view the value of the corresponding lastIndex attribute that is matched during execution.
The Code is 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 regexp.prototype.exe c (str) and String. prototype. math (rgExp)
The return value of the test method of the regular object is true or flase. This method is very useful when you only need to check whether the target string matches the specified mode, but do not need to obtain the Matching content. To obtain the matching result, use the exec (str) method of RegExp type or the match (rgExp) method of String type.
The RegExp.prototype.exe c (str) method returns NULL or returns an array. The first element of the array stores the Matching content found in the str string, one to n elements return the content of the Child matching item specified by brackets "()" in the mode.
The string.prototype.math(rgexp;#and regexp.prototype.exe c (str) behavior is similar when the marker is not used. When the global match flag is set, element 0 to n returned by the String. prototype. math (rgExp) method contains all matched items and does not contain child matches. You can use RegExp. $1... $9 to obtain 9 Child matches.