Home > Back-end >  javascript - check if either one sentence present in string
javascript - check if either one sentence present in string

Time:11-19

I have a situation here ..

If there is a , var ttext = " enzo had a pen, watch and key "

i want to check if ttext has either pen or watch or key ..

i tried using include

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

how to check multiple items efficiently .. if includes pen or watch or key .

Please help out.

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

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

want to check multiple words present

CodePudding user response:

I think you can use the or operator to verify, like this:

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

Otherwise ff you want to check if all words are contained use &&:

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

CodePudding user response:

You should use regex for this: /pen|watch|key/.test(ttext)

  • Related