javascript - Why `pattern.test(name)` opposite results on consecutive calls -
this question has answer here:
why code returning first true, false
var pattern = new regexp("mstea", 'gi'), name = "amanda olmstead"; console.log('1', pattern.test(name)); console.log('1', pattern.test(name));
demo: fiddle
g
repeating searches. changes regular expression object iterator. if want use test
function check string valid according pattern, remove modifier :
var pattern = new regexp("mstea", 'i'), name = "amanda olmstead";
the test
function, contrary replace
or match
doesn't consume whole iteration, lets in "bad" state. should never use modifier when using test
function.
Comments
Post a Comment