【问题标题】:Words exists in string using javascript使用javascript的字符串中存在单词
【发布时间】:2018-11-23 08:29:45
【问题描述】:

我需要使用 javascript OR jquery 检查字符串中是否存在单词。

例如 字符串:我需要访问 W3Schools
如果我检查“需要 w3school”==> True
如果我检查“需要去 w3school”==> False
如果我检查“w3schools 访问”==> True
如果我检查“需要去”==> False

它可以是多个单词,例如 1,2 或超过 2。 简而言之,如果字符串中存在所有单词,则它应该返回“TRUE”,否则返回“FALSE”。顺序无所谓。

var dat=[];
if(str.indexOf($('#search').val()) != -1){
    dat[] = {name:str};
}

【问题讨论】:

标签: javascript jquery


【解决方案1】:

尝试关注

let str = "I need to Visit W3Schools";
str = str.toLowerCase();

function check(s) {
  return s.toLowerCase().split(" ").every(x => str.includes(x));
}
console.log(check("need w3school"));
console.log(check("need go w3school"));
console.log(check("w3schools visit"));
console.log(check("need go "));

【讨论】:

    猜你喜欢
    • 2011-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多