我有一个用户输入字符串,并就这个基础上,我执行switch.For例子case语句,如果用户输入字符串Hello被认为是区分hello.Everything工作正常,但是当我把串='西姆拉天气'它需要它'嗨',因为它匹配第一。 我该如何更好地执行此操作?匹配的子用户输入
var string = 'shimla weather';
var substring = ['hi', 'hello', 'weather', 'joke'];
console.log(substring);
var doubles = substring.map(function(x) {
if(string.includes(x)){
expression = x;
}
});
console.log(expression);
switch(expression) {
case 'hi':
case 'hello'
console.log('hello');
break;
case 'weather':
console.log('weather');
break;
case 'joke':
console.log('joe');
break;
default:
}
的一种方法是从最长的'case's排序,以最短的字。 – Xufox
你没有描述所需的行为。如果有多个匹配,你希望发生什么? – Marty
是的,问题出现在多个匹配@马蒂的情况下。我想处理这个问题。 – art12345