Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda