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

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -