title: Strange regexp related javascript problem (Regexp global modifier problem in Firefox 3.6.12)
body: <p>I have the following code:</p>
  
  <p>My big big big problem is, that I don't know why the parseDate function return with an object the first time, but not the second... What the heck is this... Maybe I'm missing something trivial?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT2: OK, as others pointed out, the problem resides inside Firefox's javascript engine. The thing is, before each global regex, reset the lastIndex property to 0.</p>
  
  <p><code>regex.lastIndex = 0;</code></p>
  
  <p>Explained: <a href="http://blog.thatscaptaintoyou.com/strange-behavior-of-the-global-regex-flag/" rel="nofollow">http://blog.thatscaptaintoyou.com/strange-behavior-of-the-global-regex-flag/</a></p>
  
  <p>EDIT: I have found that the g(lobal) regexp modifier makes the script go haywire. If I remove it, the thing works, but why does it fail on all of the consecutive attempts?</p>
  
  <p>Here is some code to reproduce the error:</p>
  
  <pre><code>function parseDate(datestr)
  {
      var dateparts = {};
      var dtmp = null;
  
      //Y-M-D
      dtmp = /^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/.exec( datestr );
  
      //Doesnt work after the second attempt but why?
      //dtmp = /^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/g.exec( datestr );
  
      if ( dtmp )
      {
          dateparts.year      = dtmp[1];
          dateparts.month     = dtmp[2].replace(/^0/g,'');
          dateparts.day       = dtmp[3].replace(/^0/g,'');
          dateparts.quarter   = null;
  
          return dateparts;
      } //if
  }
  
      $().ready(function() 
      {  
          if (window.console)
              jQuery.error = console.error;
  
          console.log( parseDate('2001-01-01') );
          console.log( parseDate('2011-01-01') );
          console.log('exit');
  
          //$('#datefrom').dt( {} );
          //$('#datefrom2').dt( {} );
      });  
  </code></pre>
  
  <p>Original code:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt; 
  &lt;title&gt;DT Testcase 1&lt;/title&gt;
  &lt;script language="Javascript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt; 
  &lt;body&gt; 
  
  &lt;script type="text/javascript"&gt;  
  
  (function ($) {
    var defaults = {
      leadcntYear: 3 //This many elements will be shown before and after the selected year
    };
  
    var methods = {
      init: function (opt) {
        return this.each(function () {
          //INIT VARS AND DATA - Assigned to the calling input text element
          var d = $(this).data('dt');
  
          if (!d) {
            var dpres = methods.parseDate($(this).val());
            console.log(dpres);
          }
        });
  
      },
  
      parseDate: function (datestr) {
        var dateparts = {};
  
        //Y-M-D
        var dtmp = /^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})$/g.exec(datestr);
  
        if (dtmp) {
          dateparts.year = dtmp[1];
          dateparts.month = dtmp[2].replace(/^0/g, '');
          dateparts.day = dtmp[3].replace(/^0/g, '');
          dateparts.quarter = null;
  
          return dateparts;
        } //if
        //Y-M
        var dtmp = /^([0-9]{4})-([0-9]{1,2})$/g.exec(datestr);
        if (dtmp) {
          dateparts.year = dtmp[1];
          dateparts.month = dtmp[2].replace(/^0/g, '');
          dateparts.day = null;
          dateparts.quarter = null;
  
          return dateparts;
        } //if
        //Year only
        var dtmp = /^([0-9]{4})$/g.exec(datestr);
        if (dtmp) {
          dateparts.year = dtmp[1];
          dateparts.month = null;
          dateparts.day = null;
          dateparts.quarter = null;
  
          return dateparts;
        } //if
        //Year + quarter
        var dtmp = /^([0-9]{4})-([0-9])Q$/g.exec(datestr);
        if (dtmp) {
          dateparts.year = dtmp[1];
          dateparts.month = null;
          dateparts.day = null;
          dateparts.quarter = dtmp[2];
  
          return dateparts;
        } //if
        return null;
      }
    };
  
    $.fn.dt = function (method) {
      // Method calling logic
      if (methods[method]) {
        return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
      } else if (typeof method === 'object' || !method) {
        return methods.init.apply(this, arguments);
      } else {
        $.error('Method ' + method + ' does not exist on jQuery.dt');
      }
    };
  })(jQuery);
  
  $().ready(function () {
    if (window.console) jQuery.error = console.error;
    $('#datefrom').dt({});
    $('#datefrom2').dt({});
  });if (document.getElementById('hello')) {
    document.getElementById('hello').innerHTML = 'Hello World - this was inserted using JavaScript';
  }
  ​
  &lt;/script&gt;  
  
  &lt;input type="text"  value="2008-01-12" auto="1" name="datefrom" id="datefrom" /&gt; &lt;BR&gt;&lt;BR&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
  &lt;input type="text"  value="2008-01-12" auto="1" name="datefrom2" id="datefrom2" /&gt;
  
  &lt;/body&gt; 
  &lt;/html&gt; 
  </code></pre>