A simple use is to determine whether the input is a number:
INPUT1 = ' 0281234567 '; input2 = ' 0282345678 '; var reg =/^\d+$/g;
Reg.test (INPUT1); True
The value returned when the second Test was found was incorrect. Excluding writing errors, value failures, and so on all kinds of interference factors, found that only Reg is the second execution of the time can not be executed correctly. I have not encountered this problem before, then searched the relevant information.
It turns out that this problem was actually caused by/g, and this time it was discovered that, in fact, I misused a/g. Because the/g represents a global match, there is a lastindex to record the last matching position within the regular time. When repeated calls, will then continue to match the last lastindex, which will result in errors in judgment. Understand the principle of the solution is very clear:
1. Remove/g, do not need to/g to add/g logo
2. Before the second match, manually lastindex 0, in the above example: Reg.lastindex () =0 can be.
The above mentioned is a small set of JavaScript to introduce the misuse of/g caused by the regular test () can not be correctly repeated implementation of the solution, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!