Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/406.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 检查单词列表是否为字符串(小型聊天机器人)_Javascript_Jquery_Chatbot - Fatal编程技术网

Javascript 检查单词列表是否为字符串(小型聊天机器人)

Javascript 检查单词列表是否为字符串(小型聊天机器人),javascript,jquery,chatbot,Javascript,Jquery,Chatbot,为了在JS中构建一个小chabot,我需要检查我放在列表中的单词是否是字符串,如下所示: var helloWords=[“你好”、“敬礼”、“你好”、“哟”、“嘿”] var HowWords=[“你好”、“最近怎么样”、“进展如何”、“你好”] 如果“helloWords中的一个单词在字符串中” ->答复 如果“howWords中的一个单词在字符串中” ->回答其他问题 我目前正在使用下面的方法,但它根本不实用,我正在一个很长的if/else程序中迷失 var hello=/\bhello\

为了在JS中构建一个小chabot,我需要检查我放在列表中的单词是否是字符串,如下所示:

var helloWords=[“你好”、“敬礼”、“你好”、“哟”、“嘿”]

var HowWords=[“你好”、“最近怎么样”、“进展如何”、“你好”]

如果“helloWords中的一个单词在字符串中”

->答复

如果“howWords中的一个单词在字符串中”

->回答其他问题

我目前正在使用下面的方法,但它根本不实用,我正在一个很长的if/else程序中迷失

var hello=/\bhello\b |\bhi\b |\byo\b |\bsalut\b/gi.test(命令)

如果(hello==true}

你知道有没有一种更干净更有效的方法来建造这样的东西吗?也许用另一种语言

非常感谢!

您可以使用

要匹配整个字符串,请执行以下操作:

var helloWords = ["hello", "salut", "hi", "yo", "hey"];

var HowWords = ["how are you", "what's up", "how is it going", "how do you do"];

if (helloWords.includes(yourString.toLowerCase())) {
    // Reply something
}
if (HowWords.includes(yourString.toLowerCase())) {

    // Reply something else
}
要匹配部分字符串,您需要使用以下方法执行类似操作:

你可以用

要匹配整个字符串,请执行以下操作:

var helloWords = ["hello", "salut", "hi", "yo", "hey"];

var HowWords = ["how are you", "what's up", "how is it going", "how do you do"];

if (helloWords.includes(yourString.toLowerCase())) {
    // Reply something
}
if (HowWords.includes(yourString.toLowerCase())) {

    // Reply something else
}
要匹配部分字符串,您需要使用以下方法执行类似操作:

我建议您提高浏览器兼容性:

var helloWords = ["hello", "salut", "hi", "yo", "hey"]; var HowWords = ["how are you", "what's up", "how is it going", "how do you do"]; if (helloWords.indexOf(yourString.toLowerCase()) !== -1) { // Logic } if (HowWords.indexOf(yourString.toLowerCase()) !== -1) { // Logic } 我建议您提高浏览器兼容性:

var helloWords = ["hello", "salut", "hi", "yo", "hey"]; var HowWords = ["how are you", "what's up", "how is it going", "how do you do"]; if (helloWords.indexOf(yourString.toLowerCase()) !== -1) { // Logic } if (HowWords.indexOf(yourString.toLowerCase()) !== -1) { // Logic }
我会在你的字符串中添加
toLowerCase
,只是为了让它更容易工作。当字符串只包含单词时,它就起作用了。比如:“Hello”就是“Hello world”不起作用你知道为什么吗?@Enhancer我更新了答案,我添加了一种过滤部分内容的方法string@Ele你说得对,
filter
不是最好的选择。我改成了
some
我会在你的字符串中添加
toLowerCase
,只是为了让它更容易工作。当字符串只包含单词时,它就工作了。比如:“Hello”“Hello world”不起作用你知道为什么吗?@Enhancer我更新了答案,我添加了一种过滤部分内容的方法string@Ele你说得对,
filter
不是最好的选择。我改成了
some
Hello,谢谢你的帮助。但是我遇到了一个问题,只有当只有单词(例如)“Hello”时它才起作用但是如果字符串是“hello you”,它不起作用,我将使用以下代码获取字符串,可能是.val出了问题:var commands2=$(“.letsTypeIt”).val();@Enhancer查看更新后的答案,该方法不会创建新数组,只需验证数组中的一个单词即可。感谢您的帮助和宝贵的时间:)你好,谢谢你的帮助。但是我有一个问题,它只在只有单词(例如)“hello”时才起作用,但是如果字符串是“hello you”,它就不起作用了,我用下面的代码得到字符串,可能是.val的问题:var commands2=$(“.letsTypeIt”).val()@Enhancer查看更新的答案,该方法不会创建新的数组,只需验证数组中是否有一个单词。感谢您的帮助和时间:)