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

213
Vistas
How to get Word before matching word using regex Javascript?

I've been trying to extract the value before the match. For example, I have the following sentences:

Florida 03/22/2022 04/05/2022 Bid Support Help Desk is glad to assist you

Florida 04/05/2022 Bid Support Help Desk is glad to assist you

In these examples, I want to extract anything before the last occurrence of the date. So, the last occurrence of date is: 04/05/2022 and I want to extract

03/22/2022
Florida

because it is behind the matching date. I have been able to get the last occurrence of the date by the following regex:

var Date = /(\b[0-9]{2}([\-/ \.])[0-9]{2}[\-/ \.][0-9]{4}\b)(?![A-Z]\1)\b(?!.*\b\2\b)/gs.exec(String)[0];

Any guidance would be much appreciated.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use

\S+(?=\s+\b(\d{2}([-/ .])\d{2}\2\d{4})\b(?!.*\b\d{2}([-/ .])\d{2}\3\d{4}\b))

See the regex demo. Details:

  • \S+ - one or more whitespaces
  • (?= - a positive lookahead that requires the following patterns to match immediately to the right of the current location:
  • \s+ one or more whitespaces
  • \b(\d{2}([-/ .])\d{2}\2\d{4})\b - Group1: word boundary, two digits, a separator (captured in Group 2), two digits, the same separator char as in Group 2, four digits, word boundary
  • (?!.*\b\d{2}([-/ .])\d{2}\3\d{4}\b) - a negative lookahead failing the match if there are zero or more chars other than line break chars as many as possible followed with word boundary, two digits, a separator (captured in Group 3), two digits, the same separator char as in Group 3, four digits, word boundary
  • ) - end of the positive lookahead.

const texts = ['Florida 03/22/2022 04/05/2022 Bid Support Help Desk is glad to assist you','Florida 04/05/2022 Bid Support Help Desk is glad to assist you'];
const reg = /\S+(?=\s+\b(\d{2}([-/ .])\d{2}\2\d{4})\b(?!.*\b\d{2}([-/ .])\d{2}\3\d{4}\b))/;
for (const text of texts) {
  console.log(text, '=>', text.match(reg)?.[0]);
}

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