freeCodeCamp/curriculum/challenges/english/02-javascript-algorithms-an.../regular-expressions/match-characters-that-occur...

2.7 KiB

id title challengeType
587d7db6367417b2b2512b9a Match Characters that Occur Zero or More Times 1

Description

The last challenge used the plus + sign to look for characters that occur one or more times. There's also an option that matches characters that occur zero or more times. The character to do this is the asterisk or star: *.
let soccerWord = "gooooooooal!";
let gPhrase = "gut feeling";
let oPhrase = "over the moon";
let goRegex = /go*/;
soccerWord.match(goRegex); // Returns ["goooooooo"]
gPhrase.match(goRegex); // Returns ["g"]
oPhrase.match(goRegex); // Returns null

Instructions

For this challenge, chewieQuote has been initialized as "Aaaaaaaaaaaaaaaarrrgh!" behind the scenes. Create a regex chewieRegex that uses the * character to match an uppercase "A" character immediately followed by zero or more lowercase "a" characters in chewieQuote. Your regex does not need flags or character classes, and it should not match any of the other quotes.

Tests

tests:
  - text: Your regex <code>chewieRegex</code> should use the <code>*</code> character to match zero or more <code>a</code> characters.
    testString: assert(/\*/.test(chewieRegex.source));
  - text: Your regex should match <code>"A"</code> in <code>chewieQuote</code>.
    testString: assert(result[0][0] === 'A');
  - text: Your regex should match <code>"Aaaaaaaaaaaaaaaa"</code> in <code>chewieQuote</code>.
    testString: assert(result[0] === 'Aaaaaaaaaaaaaaaa');
  - text: Your regex <code>chewieRegex</code> should match 16 characters in <code>chewieQuote</code>.
    testString: assert(result[0].length === 16);
  - text: Your regex should not match any characters in "He made a fair move. Screaming about it can't help you."
    testString: assert(!"He made a fair move. Screaming about it can't help you.".match(chewieRegex));
  - text: Your regex should not match any characters in "Let him have it. It's not wise to upset a Wookiee."
    testString: assert(!"Let him have it. It's not wise to upset a Wookiee.".match(chewieRegex));

Challenge Seed

let chewieRegex = /change/; // Only change this line
let result = chewieQuote.match(chewieRegex);

Before Test

const chewieQuote = "Aaaaaaaaaaaaaaaarrrgh!";

Solution

  let chewieRegex = /Aa*/;
  let result = chewieQuote.match(chewieRegex);