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.