1

I have a string of addresses:

let addr3 =
  "123 Main Street St. Louisville OH 43071,432 Main Long Road St. Louisville OH 43071,786 High Street Pollocksville NY 56432,54 Holy Grail Street Niagara Town ZP 32908,3200 Main Rd. Bern AE 56210,1 Gordon St. Atlanta RE 13000,10 Pussy Cat Rd. Chicago EX 34342,10";

I want to use Regex with a lookahead and an exec function (I know there are different ways to do this, but I need to use these) to match each address line like the following:

['123 Main Street St. Louisville OH 43071,432',
'Main Long Road St. Louisville OH 43071,786',
'High Street Pollocksville NY 56432,54', ... ] 

I put together the following Regex expression:

/(?:^|,\d+.|,\d+)(?=([^,]+,\d+))/;

While this expression matches each of the address string above when I use it in Rubular, when I attempt to run this from VScode in the following way:

let match;
const matches = [];
const pattern = /(?:^|,\d+.|,\d+)(?=([^,]+,\d+))/;
while ((match = pattern.exec(addr3))) {
  matches.push(match[1]);
}
console.log(matches);

I get a FATAL ERROR and the text won't compile:

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory

Why will this regex combo work in Rubular, but not won't I run the exec loop, and how can I revise the code so that it will work properly?

Dog
  • 2,726
  • 7
  • 29
  • 66
  • 2
    Every call to `pattern.exec()` starts over from the beginning of the string. – Pointy Feb 11 '19 at 03:39
  • 1
    Make the pattern global instead, and (as in the linked answer) either manually increment `lastIndex`, or consume at least one character (eg add `.` to the end of the pattern), eg `/(?:^|,\d+.|,\d+)(?=([^,]+,\d+))./g` – CertainPerformance Feb 11 '19 at 03:42
  • 1
    Works for me https://jsfiddle.net/mp7qka0g/ (though the lookahead is a bit odd, unless you're expecting overlapping matches, you can remove it entirely and use a plain capture group instead) – CertainPerformance Feb 11 '19 at 03:45
  • why is it necessary to add . to the end of the pattern if that should already be accounted for in the lookahead with ',\d+.' – Dog Feb 11 '19 at 03:48

0 Answers0