javascript - 如何检查一个单词是否有特定的字母表
问题描述
Afetr 字符串被 '/' 分割,它进入 'includes' 方法。
包含的条件具有“头”。
即使 str 具有 'big head' ,它也有单词 'head'
如果在这种情况下匹配,我想得到 'true'。
它如何解决这个问题?
非常感谢您阅读本文。
const str = "big head/smallmouse/car";
const strSplited = str.split("/");
const condition = ['head','hand','foot']
console.log(condition.includes(strSplited[0]));
解决方案
您可以通过修改逻辑以在condition
数组中搜索value
包含在"big head"
(ie strSplited[0]
) 字符串中的项目来实现此目的。
在看起来像这样的代码中:
const str = "big head/smallmouse/car";
const strSplited = str.split("/");
const condition = ['head','hand','foot']
const [ bigHeadString ] = strSplited;
const result = condition.some(value => bigHeadString.includes(value));
console.log(`condition array contains match of ${strSplited[0]}:`, result);