Forum Discussion

guilherme_p's avatar
guilherme_p
Community Member
12 days ago
Solved

Check Key words in a text entry with JavaScript

Hello Guys! I,m Guio! I was trying to make a texty entry quiz and my goal was to check if the answer is corret by checking if they used some key words. I found this guy on youtube (https://yout...
  • Nedim's avatar
    12 days ago

    Copy and paste the code below exactly as it is:

    var answer = getVar("TextEntry2");
    var words = ["cat", "dog"];
    var containsWords = words.every(word => new RegExp(`\\b${word}\\b`,'i').test(answer));
    setVar("key_words1",containsWords);

    Issue: ${word} doesn’t interpolate inside quotes, so the regex looks for the literal text ${word} instead of the actual word.
    Fix: Use backticks (template literals) so word is injected correctly.

    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals