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

366
Visualizações
how to Extract word combination from an string

want to find a way to search for words in a string and return them In the same order so here is an example I am searching for dog and cat :
let story = "The dog ran away, The cat is unhappy,cat watched the sky and saw adog"
result should be :
return dog cat cat dog notice the last one on the story string is "adog" not a "dog" we just want to return the value whenever the dog combination appears.

a simple summary of the text above:
how to return a specific combination of characters in a string when they are surrounded by other characters.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use regular expressions, using the | to separate the strings to search for.

The match() method retrieves the result of matching a string against a regular expression.

let story = "The dog ran away, The cat is unhappy,cat watched the sky and saw adog" 

const search = /dog|cat/g;

console.log(story.match(search));

// will result in ["dog", "cat", "cat", "dog"]

about 4 years ago · Juan Pablo Isaza Relatório

0

In string format:

let str = 'a dog see a cat, also cat and adog ..';

let filtered = str.split(' ').filter(x => x.match(/(dog|cat)/g)).join(' ');

console.log(filtered.match(/(dog|cat)/g).join(','));

In array format:

let str = 'a dog see a cat, also cat and adog ..';

let filtered = str.split(' ').filter(x => x.match(/(dog|cat)/g)).join(' ');

console.log(filtered.match(/(dog|cat)/g));

Here, see the first comment:

find and return string with a specific word in it in js

about 4 years ago · Juan Pablo Isaza 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