A simple way to use regularity to determine whether the input is a number:
input1 = '0281234567';input2 = '0282345678';var reg = /^/d+$/g;reg.test(input1); //truereg.test(input2); //false
I found that the value returned during the second test was incorrect. After excluding various interference factors such as writing errors, failure to obtain value, etc., I found that the reg regular cannot be executed correctly only during the second execution of the second execution of the reg. I have not encountered this problem before, so I searched for relevant information.
It turns out that this problem is actually caused by /g, and I found out that I actually misused /g for this rule. Because /g represents global matching, there is a lastIndex inside when judging the rule to record the last match position. When repeated calls, the lastIndex will continue to match, which will lead to a judgment error. If you understand the principle and solve it, it will be clear:
1. Remove /g, and do not add /g mark at will if /g is not needed
2. Before the second match, manually set lastIndex to 0. For example above: reg.lastIndex()=0.
The above is the solution that the regular test() cannot be executed correctly due to misuse of /g in JavaScript introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!