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

140
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 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