Forum Discussion
guilherme_p
12 days agoCommunity Member
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...
- 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
Nedim
12 days agoCommunity Member
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
guilherme_p
11 days agoCommunity Member
It worked!
Thank you so much! 😁
Related Content
- 5 days ago
- 11 months ago