0

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")
Jimmyt1988
  • 20,466
  • 41
  • 133
  • 233
  • `"\n"` is not a two char text. It is a line feed char, LF. So, when testing outside of code, use a line break, not `\n` text. Never throw in string literals where literal text is supposed to be. And just remove the `(?=\\n)` and use a regex literal. `var reg = /(?<=Closing Date: ).+/`, for example. – Wiktor Stribiżew Apr 21 '22 at 20:18

3 Answers3

1

(?<=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

njfamirm
  • 164
  • 2
  • 12
Hein
  • 174
  • 1
  • 10
1

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)
Dave Pritlove
  • 2,601
  • 3
  • 15
  • 14
1

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);
LironZ
  • 177
  • 2
  • 8
  • Thank you. This one actually doesn't quite return only dates, but it's a good basis. – Jimmyt1988 Apr 22 '22 at 10:23
  • 1
    Apparently, here is some difference between .replace() and .split() If I remove named-captured-groups names it will work with split. will update my answer. – LironZ Apr 22 '22 at 10:54