In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail about the impact of lastIndex on regular results. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
When you encounter a problem, 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.lastIndex
2. Set lastIndex = 0 manually after test ()
This is the end of the article on "what is the impact of lastIndex on regular results". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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: 289
*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.