0
Hello, searching on Regular Expression, I got a link to the MDN: [1]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test, But I looked, I looked and I didn’t understand why the example tested twice each variable, and why the last test returned false. The part of the example given, which I need to understand is:
var regex1 = RegExp('foo*');
var regex2 = RegExp('foo*','g');
var str1 = 'table football';
console.log(regex1.test(str1));
// expected output: true
console.log(regex1.test(str1)); // porque testar de novo?
// expected output: true
console.log(regex2.test(str1));
// expected output: true
console.log(regex2.test(str1)); // por que testar de novo e por que retornou false?
// expected output: false
The flag
g
lobal serves for the pointer to advance each search. If you search for "X" in "AXBXCXDEFG" it will return true, true, true, false pq finds 3 times the value.– Bacco