Regarding the problem of string test division in regular expressions, we generally have two methods: test() and exec(). I will not elaborate on the difference between these two methods. The main thing is that test() returns a boolean value and exec() returns a matching character array. However, today I encountered a problem. The program is very simple, but the problem inside makes me unable to understand. I didn’t find the answers online (personally compared).
The code copy is as follows:
var pattern=new RegExp("match","ig");
alert(pattern.test("Match"));
alert(pattern.exec("Match"));
The value of the first alert() is true as expected
But the second test value is unexpectedly null
I don’t know what the principle is about this question, because I didn’t see the function source code. If a friend can point it out, I’m grateful! !