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粉5634465792023-09-11 20:04:08
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.