Parsing airline reservation using JS regular expressions 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.
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=&gt;s.split('').reverse().join('').trim();

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

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