Match Characters that Occur Zero or More Times 4246

Tell us what’s happening:
just not able to take what theory is providing

can you please explain what the tittle means and

Can you please explain the cases which are shown.

Your code so far


let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
let chewieRegex = /Aa*/; // Change this line
let result = chewieQuote.match(chewieRegex);

let chewieQuote = "aaaaaaaaaAAAAaaaaaarrrgh!";
let chewieRegex = /a*A*/; // Change this line
let result = chewieQuote.match(chewieRegex);
console.log(result); // Returns 'aaaaaaaaaAAAA'


let soccerWord = "gooooooooal!";
let goRegex = /go*/;
oPhrase.match(goRegex); // Returns null

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/regular-expressions/match-characters-that-occur-zero-or-more-times

The instruction/description notes that there is a quantifier that matches 0 or more of the preceding token. So naturally the idea of the lesson is to use that quantifier.

0 or more means that the regex will match for any number of the token, including its absence (0 occurences).

Compare this with the preceding lesson that introduced the ‘1 or more’ quantifier. In that case, the regex only matched if the token exists, and matches for any number of the token. The 0 or more token works in a similar fashion, but the token being absent does not cause the match to fail.

let chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";
let chewieRegex = /Aa*/; // Change this line
let result = chewieQuote.match(chewieRegex);
// result[0] is 'Aaaaaaaaaaaaaaaa'

Your regex looks for an explicit upper case A, optionally followed by any number of lower case a characters. There is one such result in the provided string.

let chewieQuote = "aaaaaaaaaAAAAaaaaaarrrgh!";
let chewieRegex = /a*A*/; // Change this line
let result = chewieQuote.match(chewieRegex);
console.log(result); // Returns 'aaaaaaaaaAAAA'

Your regex looks for any number of lowercase a characters followed by any number of uppercase A characters. The first matching sequence is returned.

(if you enabled the global flag, the second lower case a sequence would also be matched!)

let soccerWord = "gooooooooal!";
let goRegex = /go*/;
oPhrase.match(goRegex); // Returns null

Your code as provided matches against an undeclared variable, oPhrase. If you correct it to

let soccerWord = "gooooooooal!";
let goRegex = /go*/;
console.log(soccerWord.match(goRegex));

You’ll get the goooooooo match you might have expected.

Regular expressions are difficult. Check out the MDN page on regular expressions as a reference. It’s the best guide I know to learning and writing RegEx.