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

135
Vistas
Regex to capture string between string into array in js

If we have the following string:

"hukhkhk\nClosing Date: Friday 22nd April 2022\nwdwqdwqdwqd\nClosing Date: Friday 22nd April 2023\newdewfewf"

and we want to get the 2 dates from it "Friday 22nd April 2022" and "Friday 22nd April 2023"

how do I go about getting an array back of all captured strings using js.

[ "Friday 22nd April 2022", "Friday 22nd April 2023" ]

There is an issue in my regex but not sure how to fix it so that it doesn't match everything in between the first \n it finds:

(?<=Closing Date:\s)(.*)(?=\\n)

https://regexr.com/6k2uf

The js I tried is this:

var reg = new RegExp('(?<=Closing Date: )(.*)(?=\\n)/g');
reg.exec("hukhkhk\nClosing Date: Friday 22nd April 2022\nwdwqdwqdwqd\nClosing Date: Friday 22nd April 2023\newdewfewf")
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

An alternative approach for your example data, and any data with an easily definable marker for the required text, would be to split the data into an array of lines (demarked at \n), apply an array filter to the array to remove elements not containing the marker string, and finally .map the array to contain just the required part following the marker string.

The processes can be strung together in a single line as demonstrated in the following snippet:

string = "hukhkhk\nClosing Date: Friday 22nd April 2022\nwdwqdwqdwqd\nClosing Date: Friday 22nd April 2023\newdewfewf";

markerString = "Closing Date: ";

datesArray = string.split('\n').filter(text => text.indexOf(markerString) > -1).map(text => text.slice(text.indexOf(markerString)+ markerString.length));

console.log(datesArray)

about 4 years ago · Juan Pablo Isaza Denunciar

0

(?<=Closing Date: )(.*?)(?=\\n)

Starts matching after Closing Date: (space included)

Stops matching before the first \n it encounters

This worked on your example

live example

about 4 years ago · Juan Pablo Isaza Denunciar

0

Edit:

  1. I had to remove group names (were unused and making a breaking difference on runtime)
  2. filtering empty strings (@input edges) as a result of split()
const input = "hukhkhk\\nClosing Date: Friday 22nd April 2022\\nwdwqdwqdwqd\\nClosing Date: Friday 22nd April 2023\\newdewfewf";
const regex = /(?:(?:\\n|^).*?(?:\\n|$))(?:Closing\sDate:\s)?/mg
const dates = input.split(regex).filter(date => date.length > 0);
console.log(dates);

Out:

[ 'Friday 22nd April 2022', 'Friday 22nd April 2023' ]

1st answer:

Using regex to find all non-date content:

const re = /(?<junk>(?:\\n|^).*?(?:\\n|$))(?<delimiter>Closing Date: )?/gm

Then using JS split to get array with dates only

const dates = input.split(re);
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