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

SV: [jslint] true and false on same regexp test and string

Expand Messages
  • Jakob Kruse
    A RegExp object ”remembers” the last search. Try alerting hr.lastIndex. So you getting first true and then false is an indication that there are no matches
    Message 1 of 4 , Jun 17, 2009
    • 0 Attachment
      A RegExp object ”remembers” the last search. Try alerting hr.lastIndex. So you getting first true and then false is an indication that there are no matches after the end of the first match.

      Setting lastIndex to 0 produces the result you expect. I don’t know if there are other ways of resetting a regexp object?

      /Jakob

      Fra: jslint_com@yahoogroups.com [mailto:jslint_com@yahoogroups.com] På vegne af christian.wirkus
      Sendt: 17. juni 2009 16:51
      Til: jslint_com@yahoogroups.com
      Emne: [jslint] true and false on same regexp test and string





      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>

      [Non-text portions of this message have been removed]
    • Marcel Duran
      You can either set lastIndex property to 0 as Jakob suggested or redefine your RegExp object, I believe redefining costs more though, specially within a for
      Message 2 of 4 , Jun 17, 2009
      • 0 Attachment
        You can either set lastIndex property to 0 as Jakob suggested or redefine
        your RegExp object, I believe redefining costs more though, specially within
        a for loop.

        On Wed, Jun 17, 2009 at 12:10 PM, Jakob Kruse <kruse@...> wrote:

        >
        >
        > A RegExp object �remembers� the last search. Try alerting hr.lastIndex. So
        > you getting first true and then false is an indication that there are no
        > matches after the end of the first match.
        >
        > Setting lastIndex to 0 produces the result you expect. I don�t know if
        > there are other ways of resetting a regexp object?
        >
        > /Jakob
        >
        > Fra: jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com> [mailto:
        > jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>] P� vegne af
        > christian.wirkus
        > Sendt: 17. juni 2009 16:51
        > Til: jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>
        > Emne: [jslint] true and false on same regexp test and string
        >
        >
        > 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>
        >
        > [Non-text portions of this message have been removed]
        >
        >
        >



        --
        Marcel Duran


        [Non-text portions of this message have been removed]
      • sandyhead25
        This is going to sound a bit labor intensive, but my solution to finding tags is mostly flawless. Here is the logic I used in my recent context relevant and
        Message 3 of 4 , Jun 17, 2009
        • 0 Attachment
          This is going to sound a bit labor intensive, but my solution to finding tags is mostly flawless. Here is the logic I used in my recent context relevant and vocabulary independent markup beautifier written in JavaScript:

          1) Minify the input only so that whitespace is tokenized. This prevents whitespace from providing interference with the following steps.

          2) Split the minified input per character, such as:
          x = x.split('');

          3) Create a loop to cycle through the input array. In this loop identify what opening delimiters you will accept. If you looking for a single specific tag then make the tag name part of the opening delimiter, such as: '<hr'

          4) In each condition where an opening delimiter is identified you must execute a function that accepts the closing delimiter as an argument and gathers all characters between the start delimiter, the end delimiter, and does not end prematurely.

          That should be all you need to grab markup tags. This example is illustrated in the elements function of my markup_beauty application.
          http://mailmarkup.org/prettydiff/markup_beauty.js

          I am still pretty new to JavaScript with only little more than a year's experience, so don't assume that because I got the logic to work the way I wanted that my method is the most efficient path to the objective. For all I know my attempt at this could be trash, but it does work the way I want it to.

          --- In jslint_com@yahoogroups.com, Marcel Duran <contact@...> wrote:
          >
          > You can either set lastIndex property to 0 as Jakob suggested or redefine
          > your RegExp object, I believe redefining costs more though, specially within
          > a for loop.
          >
          > On Wed, Jun 17, 2009 at 12:10 PM, Jakob Kruse <kruse@...> wrote:
          >
          > >
          > >
          > > A RegExp object "remembers" the last search. Try alerting hr.lastIndex. So
          > > you getting first true and then false is an indication that there are no
          > > matches after the end of the first match.
          > >
          > > Setting lastIndex to 0 produces the result you expect. I don't know if
          > > there are other ways of resetting a regexp object?
          > >
          > > /Jakob
          > >
          > > Fra: jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com> [mailto:
          > > jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>] På vegne af
          > > christian.wirkus
          > > Sendt: 17. juni 2009 16:51
          > > Til: jslint_com@yahoogroups.com <jslint_com%40yahoogroups.com>
          > > Emne: [jslint] true and false on same regexp test and string
          > >
          > >
          > > 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>
          > >
          > > [Non-text portions of this message have been removed]
          > >
          > >
          > >
          >
          >
          >
          > --
          > Marcel Duran
          >
          >
          > [Non-text portions of this message have been removed]
          >
        • christian.wirkus
          Hey, thanks. That worked. To sandyhead25: I wanted to use the same regexp to test and to replace with. Your method is pretty laborious I guess. But thank you
          Message 4 of 4 , Jun 18, 2009
          • 0 Attachment
            Hey, thanks. That worked.
            To sandyhead25: I wanted to use the same regexp to test and to replace with. Your method is pretty laborious I guess. But thank you too. For some time now I wanted to write an XML-validator method, I think your method might be handy for that.
          Your message has been successfully submitted and would be delivered to recipients shortly.