In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the impact of lastIndex on regular expression results, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
Preface
A problem encountered today is that true and false are returned alternately when the same string is checked with a regular expression. In desperation, I reread 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 in the authoritative guide as follows: it is a read / write integer. If the matching pattern has a g modifier, this attribute is stored in the entire string at the beginning of the next index, which is used by exec () and test (). Again in the above example, take a look at the lastIndex property
Let reg = / [\ d] / index / after the modifier g//undefinedreg.lastIndex//0reg.test (1) / / truereg.lastIndex// matches once, the lastIndex changes / / 1reg.test (1) / / matches / / falsereg.lastIndex//0reg.test (1) / / truereg.lastIndex//1 from index 1
After the first successful match with test (), lastIndex is set to the end of the match, which is 1; the second time test (), the match starts with index 1, and the match fails, and the lastIndex is reset to 0. As a result, the matching result is not as expected.
Solve
1. Do not use the g modifier
Reg = / [\ d] / [\ d] / reg.test (1) / / truereg.test (1) / / truereg.lastIndex//0reg.test (1) / / truereg.lastIndex0
2. Set lastIndex = 0 manually after test ()
Analysis on the lastIndex attribute of regular expression object
There are two ways to use regular expressions in js, one is regular expression object method, the other is string object method, the former has two methods: exec (str) and test (str), and the latter has four methods: match (regexp), replace (regexp), search (regexp) and split (search). When using a method as a regular expression object, pay special attention to its lastIndex property.
Var regexp = / abcd/g;var str = 'abcdefg';alert (regexp.test (str)); / / truealert (regexp.test (str)); / / falsealert (regexp.test (str)); / / true
The result of the above code is to pop up true, false, and true, respectively. Isn't it a little confusing considering that you are using the same regular pattern?
In fact, this is what is happening with the lastIndex property of the regular expression object. LastIndex is literally the last index, in fact, it means the index position where the regular expression starts the next search, the first time it is always 0, when the first search is finished, the value of lastIndex will be set to match the index position of the last character of the string plus 1, the second search will start from the lastIndex position, and so on. If it is not found, lastIndex is reset to 0. It is important to note that the lastIndex attribute only works in regular expressions with global flags, and if we remove the g flag from the regular expression in the above code, it will pop up true three times.
The same is true of the exec () method, which returns an array where the first element of the array is the matched string, and the subsequent elements correspond to the matched strings, that is, those enclosed in parentheses in the regular expression. If the regular expression using the exec () method does not have a global flag, only the first one will be matched, and if the regular expression has a global flag, you can cycle through exec () to get all matches until exec () returns null, that is, no match can be found. The exec () method of the same regular expression can be recycled here, relying on lastIndex, because regular expressions with global flags update the value of lastIndex each time they match as a starting point for the next match.
Finally, it is important to note that the lastIndex attribute does not work in regular methods of strings, regardless of whether the regular pattern is global or not.
Thank you for reading this article carefully. I hope the article "what is the impact of lastIndex on the results of regular expressions" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.