Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Test method bug for analyzing js regularization

Shulou Source: shulou.com Published: 2022-06-02 13:33:18 09月12日 Update

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!

Tags: Regular method analysis location learning character success only string object property more index result problem help start practical no accident next Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information OPPO Reno Docker Apple macOS