javascript – check if either one sentence present in string

javascript – check if either one sentence present in string

Problem Description:

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

Solution – 1

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");

Solution – 2

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

Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject