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

84
Visualizações
javascript regex to match a combination of and/or expression

I want to match a combination of and/or expression and match only sentences in between for example

test1 test2 and test2 or test4 test5

I want to get matches test1 test2, test2, test4 test5

I tried this regex https://regex101.com/r/FFLtg6/1 but it doesn't work :

(.+)(((and|or)\s+)?)+

Update : I need a regex expression because it is part of a bigger regex.

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

0

Here's what you're looking for:

const txt = "lorem ipsum and dolor and sit or amet consecteturand elit";

console.log(txt.split(/ and | or /));

about 4 years ago · Juan Pablo Isaza Relatório

0

  1. You can use this regexp:

const text = "test1 test2 and test2 or test4 test5";
const regexp = /(?<=^|and |or ).*?(?= and| or|$)/g;

console.log(text.match(regexp));

P.S. But keep in mind that positive lookbehind feature is not supported in all browsers

  1. You can use this regexp which will not ignore all empty spaces around need parts:

const text = "   test1 test2    and    test2    or    test4 test5   ";
const regexp = /\s*(?!and|or)\b.*?(?=and|or|$)/g;

console.log(text.match(regexp));

  1. You can use this regexp which will ignore all empty spaces around need parts:

const text = "    test1 test2    and    test2    or    test4 test5     ";
const regexp = /(?!and|or|\s)\b.*?(?=\s*(and|or|$))/g;

console.log(text.match(regexp));

about 4 years ago · Juan Pablo Isaza Relatório

0

I would probably just do the obvious:

const rxWords = /\S+/g;
const rxConnectors = /^(and|or)$/i;
const nonConnector = w => ! rxConnectors.test(w) ;

function getInterestingWords( s ) {
  const corpus  = s ?? '' ;
  const matches = corpus.match( rxWords ) ;
  const words   = matches.filter( nonConnector ) ;

  return words;
}

Given that, executing

const text = 'test1 test2 and test2 or test4 test5f' ;
const interestingWords = getInterestingWords(text);

sets interestingWords to the expected

[ 'test1', 'test2', 'test2', 'test4', 'test5' ]
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