Create a dictionary Object where the propertyName is the keyword you're interested in for printing a desired message:
const responsesDictionary = {
fee: "Fee is similar to Foo",
bar: "Bar is almost like Baz",
foo: "Foo is the grandad of Fee",
"a z": "The alphabet",
};
const EL_theMsg = document.querySelector("[name=theMsg]");
const EL_sendMsg = document.querySelector('#sendMsg');
const words = Object.keys(responsesDictionary).join("|");
const rgx = new RegExp(`\b(${words})\b`, "ig");
const getResponse = () => {
const msg = EL_theMsg.value.toLowerCase();
const matches = msg.match(rgx) || [];
const response = matches.map(word => responsesDictionary[word]);
console.log(response.join("
"));
};
EL_sendMsg.addEventListener("click", getResponse);
Insert some text and use one of the words: "fee", "bar" or "foo".<br>Hit SEND for some magic<br>
<input name="theMsg" type="text" value="Test default foo message bar">
<button id="sendMsg">SEND</button>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…