Is it possible to check if part of a string matches one of multiple answers?
P粉786800174
P粉786800174 2023-09-10 21:05:01
0
1
632

There are multiple answers to a question, is it possible to check if part of a given answer is correct using regular expressions and JavaScript?

For example, the English phrase "I think about it" can be translated into Esperanto "Mi pensas pri tio" or "< em>Mi pensas al tio”. When the user writes the answer, the input text should turn red if there are any errors. For example, entering "Mi pensas" is correct.

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

P粉786800174
P粉786800174

reply all(1)
P粉563446579

If I understand correctly, this is the approach I would 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

When the text is wrong, you can set it to red.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template