In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about the role of lastIndex in regular expressions. The article is rich in content and analyzed and described from a professional point of view. I hope you can get something after reading this article.
Write the following output in turn.
Var reg1 = / a _ of _ / / falseconsole.log (reg2.test ('abcabc')); / / true
A simple regular expression test to find out if there is a character an in the string abcabc. But it turns out that there is a special false, Why?
LastIndex (for regular expressions with parameter g)
In each instantiated RegExp object, there is a lastIndex property with an initial value of 0.
/ a/.lastIndex / / 0new RegExp ('a') .lastIndex / / 0lastIndex means that when the match succeeds, the position + 1 in the original string where the last character of the match lies, that is, the index of the next character of the match (if the match is at the end of the string, the position + 1 in the original string is also returned, that is, the length of the string). If you don't have the parameter grecoery lastIndex is always 0. Var reg = / ab/g;reg.test ('123abc'); console.log (reg.lastIndex) / / 5ab/;reg.test / match content at the end var reg = / ab/g;reg.test (' 123ab'); console.log (reg.lastIndex) / / 5pm / without parameter gvar reg = / ab/;reg.test ('123abc'); console.log (reg.lastIndex) / / 0
And this lastIndex is the location where the matching starts when other matching operations are performed with the rule. Reset lastIndex to 0 when the match fails.
Var reg = / ab/g;// initial value is 0, the match is successful from the beginning, lastIndex is 4console.log (reg.test ('12ab34ab'), reg.lastIndex); / / true 4move / starting from the 4th character "3" the matching content is 8console.log (reg.test (' 12ab34ab'), reg.lastIndex). / / true 8swap / starting from bit 8 (character length 8, no bit 8) matching failed to reset lastIndex to 0console.log (reg.test ('12ab34ab'), reg.lastIndex); / / false 0swap / rematch same as step 1 console.log (reg.test (' 12ab34ab'), reg.lastIndex); / / true 4
See here, the problem will be solved, and then it will be expanded.
For reg that is not restated, it is easy to make mistakes.
/ / Test whether the strings str1 and str2 both contain the ab characters var reg = / ab/g;var str1 = '123abiding orientation var str2 =' ab123';console.log (reg.test (str1)); / / trueconsole.log (reg.test (str2)); / / false
It is obvious that there is a misjudgment here because of lastIndex. Here you can modify reg without the parameter g or redeclare reg, or you can manually modify reg.lastIndex = 0 after the first match.
Pre-check
When it comes to pre-checking, it literally means to prepare the matching query, that is, to query the next content of the matching content, but only to prepare the query to match and not to return.
Often we need to match some characters in the string followed by some characters, but we do not need to include the following characters in the matching result, for example:
Find all the characters in the following string that are followed by 2.
Var str = 'a1b2c22d31e4fg6h3mista1b2c22d31e4fg6h3room.match (/ [amurz] 2gamg); / / ["b2", "c2", "h3"]
In this way, although we can match the string with 2, we don't need the number 2, just characters. And use pre-check:
'a1b2c22d31e4fg6h3'.match (/ [a murz] (? = 2) / g); / / ["b", "c", "h"]
You can see that the conditions are fully met, but how much does the pre-check have to do with the topic of this article, lastIndex?
Let's take a look at test. As for why we use test, match matches everything until the match ends when the match is not successful, and when the match is not successful, the lastIndex is reset to 0.
Exec and test are returned when the match succeeds or fails for the first time, and does not go on to match.
Var reg1 = / [Amurz] (? = 2) / gposivar reg2 = / [aMurz] 2According to gbombomachet var str = 'a1b2c22d31e4fg6h3 console.log (reg1.test (str), reg1.lastIndex); / / true 3console.log (reg1.test (str), reg1.lastIndex); / / true 5console.log (reg1.test (str), reg1.lastIndex); / / true 16console.log (reg1.test (str), reg1.lastIndex); / / false 0console.log (reg2.test (str), reg2.lastIndex) / / true 4console.log (reg2.test (str), reg2.lastIndex); / / true 6console.log (reg2.test (str), reg2.lastIndex); / / true 17console.log (reg2.test (str), reg2.lastIndex); / / false 0
Do you see the problem? Pre-checked lastIndex does not contain pre-checked content! This can be used to simplify a lot of judgment.
For example, to match a password, we must have at least one uppercase letter, one lowercase letter, one number, at least 6 digits in length and can only be a combination of numeric letters.
According to the situation that will not be checked in advance, it will be judged like this:
/ [a murz] / .test (pwd) & & / [Amurz] / .test (pwd) & & /\ d/.test (pwd) & & / ^ [a-zA-Z0-9] {6,} $/ .test (pwd)
But:
/ ^ (? =. * [a murz]) (? =. * [A murz]) (? = .*\ d) [a-zA-Z0-9] {6,} $/ .test (pwd)
Break it down and see:
(? =. * [a murz]) whether there are lowercase letters, but if the pre-check match failed, the false success lastIndex will not change, or 0. In the same way, understand the contents of the two external pre-check, and finally match the alphanumeric combination of more than 6 feeds, but the front is always pre-checked, and the lastIndex is always not 0, and each match is matched from the beginning, so it meets the requirements.
The above is the role of lastIndex in the regular expression shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
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.