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

197
Visualizações
JS Pig poem matching substring with Regexp

I have a one long string with sentences, every sentence contains a number. Every sentence is separated by /X. Having a given number, how can I match and extract only the one sentence which contains that number?

"\X The animal 0000 I really dig \X Above all others is the pig. 222 \X Pigs are noble. Pigs are clever 3333, \X Pigs are 5555 courteous.\X However, Now and then, to break this 6666 rule, \X One meets 7777 a pig who is a fool. \X"

For example, I want to extract the sentence with the number 5555, to get this: " Pigs are 5555 courteous."

How to do this with JS regexp? My code matches the whole text:

str.match(/\\X.*5555.*\\X/);
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You need to make sure to exclude in .* the case when it would match \\X.

This worked for me:

/[^\\X]*5555[^\\X]*/g

Use this if you want to exclude the first space of your sentence, however it requires support for Lookbehind:

/(?<=\\X )[^\\X]*5555[^\\X]*/g
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