Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Test method bug for analyzing js regularization

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "analyzing js regular test method bug". In daily operation, I believe many people have doubts about analyzing js regular test method bug. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "analyzing js regular test method bug". Next, please follow the editor to study!

The following code is debugged under F12 of chrome. You can study it.

Let's take a look at something first.

Var re = /\ dUniverse console.log (re.test ("1")); console.log (re.test ("1"))

It's all true. No problem.

But you change /\ d _ bind; change it to /\ d _ bind g; try again.

Modify it again:

Console.log (/\ d/g.test ("1")); console.log (/\ d/g.test ("1"))

It's all true. Why on earth is that?

These results are very interesting, of course, the master naturally knows why, if you know, you can actually skip it below, it's all hydrology.

There is an attribute of lastIndex in the rule, which is the starting position of the next match.

Var re = /\ d re.test console.log (re.test ("1"), re.lastIndex); console.log (re.test ("1"), re.lastIndex)

You can see that the result of the first match is true, which means that the match is successful, and lastIndex records that the starting position of the next match is 1.

So the second match matches from the position of index 1 of the "1" string, of course, the match fails, because the string has only one character, and his index is 0.

And why does /\ d/g.test ("1") match this into a function every time?

Because it uses regular literals directly, which is equivalent to recreating a regular object at a time, the initial value of the lastIndex property is 0.

So it's a match every time.

Now do you understand, including exec, one match at a time, lastIndex records the starting position of the next match.

If you have to use a regular object, you have to reset the lastIndex before each test to ensure that he doesn't have an accident.

Var re = /\ d re.test console.log (re.test ("1")); re.lastIndex = 0 position console.log (re.test ("1")); re.lastIndex = 0 position console.log (re.test ("1")); re.lastIndex = 0 position console.log (re.test ("1"))

At this point, the study of "analyzing the test method bug of js regularization" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report