Why is this regex method not working as expected?

If you call a method test

on RegExp

, it somehow "completes" RegExp

. It seems that the subsequent exec call no longer works.

var isoDateRegEx = /(\d{4})-(\d{2})-(\d{2})/g;
var isoDateString = '2001-01-01';

if (!isoDateRegEx.test(isoDateString)) {
  throw 'isoDateString must be in ISO 8601 format.';
}

var captures = isoDateRegEx.exec(isoDateString); // null

      

+3


source to share





All Articles