Wild Card dot with g flat

Tell us what’s happening:
** Wondering Why wild card . (dot) not match sun and pun when i use g flag in the end of regex? Maybe wild card match only one character, we cant use g flag for wild card.
Its work fine without g flag. Can anyone explain plz…

Your code so far


let exampleStr = "Let's have fun with regular expressions!";
let unRegex = /un./g; // Change this line
let result = unRegex.test(exampleStr);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/match-anything-with-wildcard-period

the g flag with the test method has a different behaviour than you expect

Using test() on a regex with the global flagSection

If the regex has the global flag set, test() will advance the lastIndex of 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.

1 Like

also, you are matching un followed by any character, not .un, but un.. it means that if you have un as last lettera of the string, it will not match as there is not an other character after

1 Like

Thanks, its help me lot.