Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

358
Visualizações
How to search through a string with an array of strings (js)?

What Im trying to do is the example below (except for the part where it doesnt work): I want to filter through a string (Message), where it checks for an array of strings (Exception), with outcome true (for any of the strings from the array) or false (if there are none of the strings from the array). I cant figure out how to make this work, as most information i find seems to do only the opposite (search through an array of strings with a single string).

Any help would be greatly appreciated!

var Message = "i bought a pear";
const Trigger = "bought";
const Exception = ["apple", "pear", "banana"]

if ((Message.includes(Trigger)) && (!Message.includes(Exception))) {
    console.log("Trigger hit!");                                         
}
about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

You need to iterate the exception array and check the message.

const
    message = "i bought a pear",
    trigger = "bought",
    exception = ["apple", "pear", "banana"];

if (message.includes(trigger) && !exception.some(e => message.includes(e))) {
    console.log("Trigger hit!");                                         
}

console.log(exception.some(e => message.includes(e))); // true

about 4 years ago · Santiago Trujillo Relatório

0

    let message = "i bought a pear";
    const trigger = "bought";
    const exceptions = ["apple", "pear", "banana"];

    let newMessageArr = message.split(' ');
    exceptions.forEach((exception) => {
        console.log(exception);
        newMessageArr.forEach(word => {
            if( word === exception )
                console.log("true");
        })
    })
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda