I'm using javascript and html to develop a simple chatbot. The code below works and uses .split to check for a whole word, however, this means that anything entered longer than one word such as "how are you?" no longer works. How can change this so that it'll allow multiple words but still check for whole words like "hi" so that they aren't picked up in bigger words such as "high" etc.
Any help would be much appreciated! Thanks!
var know = {
<!--General Phrases-->
"Hi": "Hello! 👋",
"Hey": "Hello! 👋",
"Hello":"Hello 👋 How can I help?",
"how are you":"Not bad, thanks!",
"Bye":"Have a nice day!",
"Goodbye":"See you later!",
<!--Directory-->
"Help": `You can find help by searching below or by clicking <a href='https://www.page.com/news' target="_blank">here</a>`,
"contact": `You can contact us by clicking <a href='https://www.page.com/contact' target="_blank">here</a>`,
"About": `You can find our About Us page by clicking <a href='https://www.page.com/about' target="_blank">here</a>`
};
function goo() {
var userBox = document.getElementById('userBox');
var userInput = userBox.value;
var chatLog = document.getElementById('chatLog');
var chatLogContent = "";
if (!userInput) {
chatLogContent = ''
}
var hasKeyword = false;
for (var key in know) {
if (userInput.toLowerCase()
.replace(/[.,\/#!$%\^&\*;:{}=\-_`~ ()]/g,"")
.split(/\s+/)
.includes(key.toLowerCase())) {
hasKeyword = true;
break;
} else {
hasKeyword = false;
}
}
if (hasKeyword) {
chatLogContent += know[key] + "<br>"; //or use know.key
} else {
chatLogContent += "No results found. Please enter another search term below.<br>";
}
var server = document.createElement('div');
server.setAttribute('class', 'server');
server.innerHTML = chatLogContent;
document.getElementById('chatLog').innerHTML = '';
chatLog.appendChild(server);
}