【问题标题】:javascript - check if either one sentence present in stringjavascript - 检查字符串中是否存在一个句子
【发布时间】:2022-11-19 01:18:58
【问题描述】:

我这里有情况..

如果有 , var ttext = " enzo had a pen, watch and key "

我想检查文本是否有笔或手表或钥匙..

我试过使用包括

var ttext = " enzo had a pen, watch and key "
let result = ttext.includes("pen");

如何有效地检查多个项目.. 如果包括笔或手表或钥匙。

请帮忙。

var ttext = " enzo 有一支笔、手表和钥匙 "

让 result = ttext.includes("pen");

想要检查存在的多个单词

【问题讨论】:

  • 使用正则表达式 (Regex) 可以帮助您解决这个问题。你应该考虑学习它
  • 正则表达式或包含 every() 和 do 的单词数组。您的解决方案也将匹配“开放”

标签: javascript node.js string include search-multiple-words


【解决方案1】:

我认为您可以使用 or 运算符来验证,如下所示:

let result = ttext.includes("pen" || "watch" || "foo");

否则如果你想检查是否包含所有单词使用&&

let result = ttext.includes("pen" && "watch" && "foo");

【讨论】:

    猜你喜欢
    • 2016-05-24
    • 1970-01-01
    • 2021-11-28
    • 2022-12-30
    • 2011-03-24
    • 2019-05-11
    • 1970-01-01
    相关资源
    最近更新 更多