Parsing airline reservation using JS regular expressions

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












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.







share|improve this question



























    up vote
    2
    down vote

    favorite












    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.







    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      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.







      share|improve this question













      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.






      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")





      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")








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jan 10 at 18:50









      200_success

      123k14143401




      123k14143401









      asked Jan 9 at 13:28









      Julian E.

      1137




      1137




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          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);








          share|improve this answer























          • 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!
            – Julian E.
            Jan 9 at 16:18










          • @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)
            – Occam's Razor
            Jan 9 at 16:38










          • 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?
            – Julian E.
            Jan 9 at 16:39










          • @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.
            – Occam's Razor
            Jan 9 at 16:46










          • 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?
            – Julian E.
            Jan 9 at 16:48










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184653%2fparsing-airline-reservation-using-js-regular-expressions%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          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);








          share|improve this answer























          • 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!
            – Julian E.
            Jan 9 at 16:18










          • @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)
            – Occam's Razor
            Jan 9 at 16:38










          • 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?
            – Julian E.
            Jan 9 at 16:39










          • @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.
            – Occam's Razor
            Jan 9 at 16:46










          • 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?
            – Julian E.
            Jan 9 at 16:48














          up vote
          1
          down vote



          accepted










          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);








          share|improve this answer























          • 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!
            – Julian E.
            Jan 9 at 16:18










          • @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)
            – Occam's Razor
            Jan 9 at 16:38










          • 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?
            – Julian E.
            Jan 9 at 16:39










          • @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.
            – Occam's Razor
            Jan 9 at 16:46










          • 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?
            – Julian E.
            Jan 9 at 16:48












          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          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);








          share|improve this answer















          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);








          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);





          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);






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jan 10 at 15:26


























          answered Jan 9 at 14:09









          Occam's Razor

          1,982513




          1,982513











          • 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!
            – Julian E.
            Jan 9 at 16:18










          • @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)
            – Occam's Razor
            Jan 9 at 16:38










          • 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?
            – Julian E.
            Jan 9 at 16:39










          • @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.
            – Occam's Razor
            Jan 9 at 16:46










          • 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?
            – Julian E.
            Jan 9 at 16:48
















          • 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!
            – Julian E.
            Jan 9 at 16:18










          • @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)
            – Occam's Razor
            Jan 9 at 16:38










          • 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?
            – Julian E.
            Jan 9 at 16:39










          • @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.
            – Occam's Razor
            Jan 9 at 16:46










          • 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?
            – Julian E.
            Jan 9 at 16:48















          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!
          – Julian E.
          Jan 9 at 16:18




          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!
          – Julian E.
          Jan 9 at 16:18












          @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)
          – Occam's Razor
          Jan 9 at 16:38




          @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)
          – Occam's Razor
          Jan 9 at 16:38












          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?
          – Julian E.
          Jan 9 at 16:39




          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?
          – Julian E.
          Jan 9 at 16:39












          @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.
          – Occam's Razor
          Jan 9 at 16:46




          @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.
          – Occam's Razor
          Jan 9 at 16:46












          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?
          – Julian E.
          Jan 9 at 16:48




          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?
          – Julian E.
          Jan 9 at 16:48












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184653%2fparsing-airline-reservation-using-js-regular-expressions%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods