Analyze the influence of lastIndex on regular expression results,

Source: Internet
Author: User

Analyze the influence of lastIndex on regular expression results,

Preface

Today, a problem occurs. When you use a regular expression to check the same string, true and false are returned alternately. In desperation, I flipped through the authoritative guide and found that the culprit was lastIndex. You can try it on the console

let reg = /[\d]/g//undefinedreg.test(1)//truereg.test(1)//false

LastIndex

LastIndex is explained as follows in the authoritative guide: it is a readable/written integer. If the matching mode has a g modifier, this attribute is stored at the starting position of the next index of the entire string. This attribute will be used by exec () and test. In the preceding example, observe the lastIndex attribute.

Let reg =/[\ d]/g // modifier g // undefinedreg. lastIndex // 0reg. test (1) // truereg. lastIndex // after a match, lastIndex changes // 1reg. test (1) // match from index 1 // falsereg. lastIndex // 0reg. test (1) // truereg. lastIndex // 1

After the test () Match is successful for the first time, lastIndex is set to the end position of the match, that is, 1. When the test () operation is performed again for the second time, the match starts from index 1 and the match fails, reset lastIndex to 0. In this way, the matching result is inconsistent with the expectation.

Solution

 1. Do not use g Modifiers

reg = /[\d]////[\d]/reg.test(1)//truereg.test(1)//truereg.lastIndex//0reg.test(1)//truereg.lastIndex0

 2. manually set lastIndex = 0 after test ()

Analyze the lastIndex attribute of the regular expression object

There are two ways to use regular expressions in js: one is a regular expression object method and the other is a string object method. The former has exec (str), test (str) the two methods include match (regexp), replace (regexp), search (regexp), and split (search. When used as a regular expression object method, pay special attention to its lastIndex attribute.

var regexp = /abcd/g;var str = 'abcdefg';alert(regexp.test(str)); //truealert(regexp.test(str)); //falsealert(regexp.test(str)); //true

The running results of the above Code are true, false, and true. Considering that the same regular mode is used, is it a bit confusing?

In fact, this is the lastIndex attribute of the regular expression object. LastIndex is literally the last index. In fact, it indicates the index location where the regular expression starts the next query. It is always 0 for the first query, when the first query is complete, the value of lastIndex is set to the index location of the last character matching the string plus 1. The second query starts from the location of lastIndex, and so on. If no value is found, the lastIndex is reset to 0. It should be noted that the lastIndex attribute only works in a global sign regular expression. If we remove the g sign of the regular expression in the code above, all the three pop-up events will be true.

The exec () method is the same. The exec () method returns an array. The first element of the array is the matched string, and the subsequent elements correspond to the matched strings respectively, that is, those enclosed in parentheses in the regular expression. If the regular expression using the exec () method does not have a global sign, it will only match the first one. If the regular expression has a global sign, you can use exec () cyclically to obtain all the matches, until exec () returns null, that is, no matching is found. Here, we can use the exec () method of the same regular expression cyclically, relying on lastIndex, because the regular expression with the global flag will update the value of lastIndex after each match as the starting point for the next match.

The last note is that the lastIndex attribute does not work in the string's regular expression method, regardless of whether the regular expression mode is global or not.

Summary

The above section describes the impact of lastIndex on the regular expression results. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in a timely manner. Thank you very much for your support for the help House website!

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.