Learn Regular Expressions by Building a Spam Filter - Step 19

Tell us what’s happening:

Please assist me. I am not sure what is required of me here. The error message says the capture group should be optional.

Your code so far


/* file: script.js */
const messageInput = document.getElementById("message-input");
const result = document.getElementById("result");
const checkMessageButton = document.getElementById("check-message-btn");

const helpRegex = /please help|assist me/i;

// User Editable Region

const dollarRegex = /\? [0-9]+ (hundred|thousand|million|billion) dollars/i;

// User Editable Region

const denyList = [helpRegex, dollarRegex];

const isSpam = (msg) => denyList.some((regex) => regex.test(msg));

checkMessageButton.addEventListener("click", () => {
  if (messageInput.value === "") {
    alert("Please enter a message.");

  result.textContent = isSpam(messageInput.value)
    ? "Oh no! This looks like a spam message."
    : "This message does not seem to contain any spam.";
  messageInput.value = "";

Your browser information:

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

Challenge Information:

Learn Regular Expressions by Building a Spam Filter - Step 19

Correct, that’s what the instructions are asking for. From the instructions:

“The ? quantifier matches zero or one occurrence of the preceding character or group.”

In other words, the ? makes something optional. The example shows you how to make the u optional in color (or colour). Do you understand what the ? is doing in the pattern /colou?r/?

Instead of just making a letter optional, you can also make a group of something optional, such as a capture group. Based on how the letter u is made optional in the example, how do you think you would make the capture group optional?

1 Like

i figured it out. thank you.