Loading ...
Sorry, an error occurred while loading the content.

true and false on same regexp test and string

Expand Messages
  • christian.wirkus
    Hi, this is not really a JSLint-Question, but I figured the smartest Javascript guys in this group. I wrote a RegExp that finds the hr-tag: var hr =
    Message 1 of 1 , Jun 17 7:51 AM
    • 0 Attachment
      Hi, this is not really a JSLint-Question, but I figured the smartest Javascript guys in this group.

      I wrote a RegExp that finds the hr-tag:
      var hr = /<hr[^>]*?>/ig;

      I want that to test an innerHTML string.
      <div id="foo">The quick brown fox<hr />jumps over the lazy dog.</div>
      hr.test(document.getElementById("foo").innerHTML);

      When I do the test twice in succession on the same innerHTML, then it gives me once true and once false (in Firefox3, IE6 and IE7).

      Why is that?

      Here is the complete example, it passes both jslint and w3c html validator:

      <!DOCTYPE html>
      <html>
      <head>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
      <title></title>
      </head>
      <body>

      <div id="foo">The quick brown fox<hr />jumps over the lazy dog.</div>

      <script type="text/javascript">
      var hr = /<hr[^>]*?>/ig;

      //alerts true
      alert(hr.test(document.getElementById("foo").innerHTML));

      //alerts false
      alert(hr.test(document.getElementById("foo").innerHTML));
      </script>

      </body>
      </html>
    Your message has been successfully submitted and would be delivered to recipients shortly.