title: Parsing XML in JavaScript using RegExp and exec - it usually works, but in a few cases it doesn't - any idea why?
body: <p>I am parsing some XML in server-side JavaScript. The XML is returned by a call to another server. I'm using a modified version of a solution I found at <a href="https://stackoverflow.com/questions/4341421/with-regex-how-do-i-match-between-an-xml-tag-multiple-times">With regex how do i match between an XML tag multiple times?</a> and tested the RegExp with the tester at <a href="http://www.regular-expressions.info/javascriptexample.html" rel="nofollow noreferrer">http://www.regular-expressions.info/javascriptexample.html</a>.</p>
  
  <p>There are some 1280 records returned, and it works almost all the time. But I noticed today that about 30 records don't parse correctly. Some fields end up null when parsed and I can't figure out why.</p>
  
  <p>A sample of a regular expression I'm creating in a loop is </p>
  
  <pre><code>&lt;field name="URL_ProProfs"&gt;&lt;data&gt;(.+?)&lt;\/data&gt;&lt;\/field&gt;
  </code></pre>
  
  <p>And an example of a record I'm parsing is</p>
  
  <pre><code>&lt;record mod-id="439" record-id="6640"&gt;
      &lt;field name="Name"&gt;
          &lt;data&gt;Inviting Young Children to Explore Process Art&lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="Byline_Presenter"&gt;
          &lt;data&gt; Presented by MaryAnn F. Kohl, Author, Educator, and Presenter &lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="Date"&gt;
          &lt;data&gt;11/14/2016&lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="URL_On_Demand"&gt;
          &lt;data&gt; https://www.anymeeting.com/843-066-411/E954DB85814B38 &lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="URL_ProProfs"&gt;
          &lt;data&gt; https://www.proprofs.com/quiz-school/story.php?title=quiz-18-classroommanagementwebinar20161114_3WO &lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="Status"&gt;
          &lt;data&gt;1&lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="URL_Blog"&gt;
          &lt;data&gt; http://home.edweb.net/webinar/young-children-explore-process-art/ &lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="Channel_1"&gt;
          &lt;data&gt;Arts in Education&lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="Channel_2"&gt;
          &lt;data&gt;Early Childhood Learning&lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="Channel_3"&gt;
          &lt;data/&gt;
      &lt;/field&gt;
      &lt;field name="Channel_4"&gt;
          &lt;data/&gt;
      &lt;/field&gt;
      &lt;field name="Name_Community"&gt;
          &lt;data&gt;classroommanagement&lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="Webinars_COMMUNITIES::Name_Long"&gt;
          &lt;data&gt;Classroom Management for Early Learning&lt;/data&gt;
      &lt;/field&gt;
      &lt;field name="_ID_Community"&gt;
          &lt;data&gt;.5a1dcfde&lt;/data&gt;
      &lt;/field&gt;
  &lt;/record&gt;
  </code></pre>
  
  <p>If I use that RegExp at the tester with that string it correctly parses and I am able to find the return value for that field as the 2nd element of the .exec Array result:</p>
  
  <pre><code>https://www.proprofs.com/quiz-school/story.php?title=quiz-18-classroommanagementwebinar20161114_3WO
  </code></pre>
  
  <p>But sometimes, depending on some reason I can't fathom, I'm getting null values returned for different fields I'm parsing for. In this case, for field URL_ProfProps.</p>
  
  <p>I'm using the following loop to do an exec on RegExp to find all the values in the record. The function itself is called for each record.</p>
  
  <pre><code>function webinarParse(record) {
    var r = new Object();
    var fields = ["Name", "Byline_Presenter", "Date", "URL_On_Demand", "URL_Blog", "URL_ProProfs", "Name_Community", "Webinars_COMMUNITIES::Name_Long", "Channel_1", 
                  "Channel_2", "Channel_3", "Channel_4", "_ID_Community"];
  
    for (f=0; f&lt;fields.length; f++) {
        var re = new RegExp('&lt;field name="' + fields[f] + '"&gt;&lt;data&gt;(.+?)&lt;\/data&gt;&lt;\/field&gt;');
        var m = re.exec(record);
        if (m == null) {
          r[fields[f]] = "";
        } else {
          if (fields[f] == 'Date') {
              r[fields[f]] = new Date(m[1]);
          }
          else {
              if (m[1].indexOf('&lt;data&gt;') != -1) {
                  // greedy match captured from next field when there was no data present
                  r[fields[f]] = "";
              }
              else {
                  r[fields[f]] = m[1];
              }
          }
        }
    }  
    return r;
  }
  </code></pre>
  
  <p>The parameter record is one record from all the XML records I returned.</p>
  
  <p>The Object() r is my return value.</p>
  
  <p>The Array() fields is a list of values I'm parsing for.</p>
  
  <p>In the loop I create a new RegExp for each element of fields to see if there is a value to return for it.</p>
  
  <p>If re.exec(record) is null I just set an empty string for that property in r.</p>
  
  <p>If the field happens to be a date string I set a Date object for that property.</p>
  
  <p>Then I have to be careful because the XML is returning </p>
  
  <pre><code>&lt;data /&gt; 
  </code></pre>
  
  <p>if there is no data, rather than </p>
  
  <pre><code>&lt;data&gt;&lt;/data&gt;
  </code></pre>
  
  <p>So I check for that. In other words, if there is data, there should be a </p>
  
  <pre><code>&lt;data&gt; 
  </code></pre>
  
  <p>tag. If there isn't, I don't want to grab too much.</p>
  
  <p>I'm logging the parsing of each record and it works about 1250 times out of 1280 times. But for some records, one or two of the fields are not parsing, even though I confirmed there is data there in the XML itself.</p>
  
  <p>The example above is one that does seem to parse correctly at the tester page, but in my loop the value for URL_ProPofs is returning a null value. </p>
  
  <p>Any thoughts about my RegExp or loop doing this? I just can't figure out why it sometimes just doesn't find the value.</p>
  
  <p>Thanks.</p>