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 -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -