Javascript regexp objects can only use a problem solver once
- 2020-03-30 03:27:09
- OfStack
The following code is the test to identify the date from the string, and you can see that the created rYMD RegExp object was executed once, and then again.
var DateStr = "2014-9-8";
var rYMD = new RegExp("(\d{4}|\d{2})-(\d{2}|\d{1})-(\d{2}|\d{1})", "g");
var aRt = rYMD.exec(DateStr);
var sRt=rYMD.exec(DateStr);
After debugging, it was found that on the first execution, the aRt got the returned Array (Array), but the sRt that followed it was null
On trial and error, it turns out that the RegExp object is dead after one execution.
So, it's important to note that every time you use a RegExp, you need a new one.