let username = "JackOfAllTrades";
let userCheck = /[A-Za-z][a-zA-Z]+[0-9]*$/ig;
let result = userCheck.test(username);
Output: Your regex should match JACK Your regex should match RegexGuru
While I could code a solution for the lesson, I can’t figure out why this particular Regex is not acceptable to fill the requirements.
I already tried “/^[A-Za-z][a-zA-Z]+[0-9]*$/ig” in case those strings not matched were starting with a whitespace, but it seems they don’t, for it has not worked either.
don’t use the g flag with a method before knowing what’s doing
If the regex has the global flag set, test() will advance the lastIndexof the regex. A subsequent use of test() will start the search at the substring of str specified by lastIndex (exec() will also advance the lastIndex property). It is worth noting that the lastIndex will not reset when testing a different string.
Thanks for your good-will to teach, your replies have been hitherto always very useful!
If I got it right, then a global run works by searching for a match, updating the value of lastIndex, storing the matched data, then searching again from the updated index onward, until the string ends (when lastIndex gets greater than the length of the string).
Nevertheless, I have still failed to figure out what determines lastIndex value.
var regex1 = new RegExp( "foo", "g" );
var str1 = 'table football, foosball';
regex1.test(str1);
console.log(regex1.lastIndex);
// expected output: 9
regex1.test(str1);
console.log(regex1.lastIndex);
// expected output: 19
lastIndex starts at 0, then after a string is tested, lastIndex is moved after the part of the string that tests true, foo is at indexes 6, 7 and 8. so next time it starts checking at index 9
BTW, we really could have some extra explanation on backfunctions and how things work behind the scenes. When the lesson presented g flag, it was simply like “use it to keep searching after a match”.
Community support is vital to make FCC campers functional coders, I suppose