2
\$\begingroup\$

I'm searching a set text for multiple values, which will later be stored in a database.

I am using RegExp and worked with the official documentation and help to get the code to be two separate while loops. The question is if it's possible combine those into one, adding more and more regular expressions, or if they each need a for-loop of their own.

const regex = /PREPARED FOR([^]*?)RESERVATION/g;
const regex2 = /AIRLINE RESERVATION CODE (.*)/g;

const str = `30 OCT 2017  04 NOV 2017
Distance (in Miles):500
Stop(s): 0
Distance (in Miles):500
Stop(s):0
TRIP TO KRAKOW, POLAND
PREPARED FOR
DOE/JANE MRS 
APPLESEED/JOHN MR 
RESERVATION CODE   UVWXYZ
AIRLINE RESERVATION CODE DING67 (OS)
AIRLINE RESERVATION CODE HDY75S (OS)`;
let m;
let x;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    console.log(m[1])

}

while ((x = regex2.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (x.index === regex2.lastIndex) {
        regex2.lastIndex++;
    }
    console.log(x[1])

}
console.log("We're done here")

Now, this works perfectly fine as is, but I will be adding more filters and searches to it, which means I may get to a total of 8-9 while loops, which may slow the process down, or be inefficient.

\$\endgroup\$
1
\$\begingroup\$

You don't need a loop, you can use the string's match method, but in order to get only the matched group you have to use a lookbehind, which javascript's implementation of regex doesn't support, but, it does support a lookahead, so basically, you could reverse it, reverse all the matches and filter out empty strings to get the reservation codes. Since you only have a single match for the names you don't need a loop for that either, assuming there will always be a match, so you can just use exec and pop the result off the end..

const str = `30 OCT 2017  04 NOV 2017
    Distance (in Miles):500
    Stop(s): 0
    Distance (in Miles):500
    Stop(s):0
    TRIP TO KRAKOW, POLAND
    PREPARED FOR
    DOE/JANE MRS 
    APPLESEED/JOHN MR 
    RESERVATION CODE   UVWXYZ
    AIRLINE RESERVATION CODE DING67 (OS)
    AIRLINE RESERVATION CODE HDY75S (OS)`;

var r1 = / (.*)(?= EDOC NOITAVRESER ENILRIA)/g,
    r2 = /PREPARED FOR([^]*?)RESERVATION/g;

const rev = s=>s.split('').reverse().join('').trim();

let x = r2.exec(str).pop();
let m = rev(str).match(r1).map(rev).filter(w=>w.length);

console.log(x);
console.log(m);

\$\endgroup\$
  • \$\begingroup\$ Awesome! Thank you! One question though, I will have to add more code and regex to search for departures as well as for dates and times, when there may be more than one match. Is the same applicable then, of using look ahead a or will I need to figure something else out? Thank you! \$\endgroup\$ – Julian E. Jan 9 '18 at 16:18
  • \$\begingroup\$ @JulianE. -What we're doing is looking for anything that comes after the text AIRLINE RESERVATION CODE , we're doing that by reversing both the needle and the haystack and instead searching for anything that comes before the reversed needle. If your haystack will contain DEPARTURE CODE XXX and you want to match XXX you can use the same trick: r3 = /(.*)(?= EDOC ERUTRAPED)/g <- you still need to reverse the string and then reverse the matches and filter like I showed: rev(str).match(r3).map(rev).filter(w=>w.length) \$\endgroup\$ – I wrestled a bear once. Jan 9 '18 at 16:38
  • \$\begingroup\$ Awesome! And this also works when there’ll be multiple instances of say, the departure code? And thank you very much for your help, to fully understand this. The reversing we’re doing is because of how JS implemented RegExp? \$\endgroup\$ – Julian E. Jan 9 '18 at 16:39
  • \$\begingroup\$ @JulianE. - Yes sir, the g flag means it will pull all the matches all at once, even if there's more than one. And yes, we're reversing because Javascript's implementation of RegEx doesn't include lookbehinds, but it supports lookaheads, so we reverse it and use lookaheads instead of lookbehinds. \$\endgroup\$ – I wrestled a bear once. Jan 9 '18 at 16:46
  • \$\begingroup\$ Awesome! Thank you so much!! That really helped, and thanks for the explanation! You really rock! And I’ll going to assume there’s an awesome story to your username? \$\endgroup\$ – Julian E. Jan 9 '18 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.