Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

134
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!