-2

Having multiple answers to a question, is it possible to check, using a regex and JavaScript, if part of a given answer is correct?

For example the english phrase "I think about it" can be translated into Esperanto as "Mi pensas pri tio" or "Mi pensas al tio". While the user is writing his answer, the input text should turn red, if there is any error. So for example the input "Mi pensas" is correct.

Instead of looping through all possible answers, is it possible to use a pattern like "Mi pensas (pri|al) tio"?

anvalon
  • 35
  • 7

1 Answers1

-1

If I understood correct, this is the approach I will take:

function checkInput(input) {
  const pattern = /^Mi pensas( (pri|al) tio)?$/;
  return pattern.test(input);
}

console.log(checkInput("Mi pensas pri tio"));  // true
console.log(checkInput("Mi pensas al tio"));  // true
console.log(checkInput("Mi pensas"));  // true

You can just make your text red when it is false.

Tal Biton
  • 40
  • 5