Why does JavaScript Regx behave weirdly?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Cain

    Why does JavaScript Regx behave weirdly?

    A very simple regular expression test:

    var regx = /^\d+$/g;
    alert(regx.test ("11") + ": " + regx.test("11") );

    And the output is "true: false".

    If I change to regx = new RegExp("^\\d+$" , "g");
    Then the output is the same.

    This is the first weird part.

    The second weird part is, when I only test the string once in a
    function:

    var regx = /^\d+$/g;
    alert(regx.test ("11") + ": ");

    var regx = new RegExp("^\\d+$" , "g");
    alert(regx.test ("11") + ": ");

    If I call the function twice, the output will be

    1st call:
    true, true

    2nd call:
    true, false

    My assumption is the global expression holds an internal status, but
    that doesn't explain the second example.

    Can someone help me out here please?

  • Lasse Reichstein Nielsen

    #2
    Re: Why does JavaScript Regx behave weirdly?

    Cain <starwhisperer@ gmail.comwrites :
    A very simple regular expression test:
    >
    var regx = /^\d+$/g;
    alert(regx.test ("11") + ": " + regx.test("11") );
    >
    And the output is "true: false".
    Expected behavior. When you use the "g" flag, the regexp remembers the
    position end of its match, and tries to match from that point when its
    "test" or "exec" methods are next called. It does so until it fails to
    match, at which point it resets itself so the next invocation will start
    at position 0 again.

    The second weird part is, when I only test the string once in a
    function:
    >
    var regx = /^\d+$/g;
    alert(regx.test ("11") + ": ");
    >
    var regx = new RegExp("^\\d+$" , "g");
    alert(regx.test ("11") + ": ");
    >
    If I call the function twice, the output will be
    >
    1st call:
    true, true
    >
    2nd call:
    true, false
    Your output doesn't match what you wrote above.

    Anyway: A regexp literal (/whatnot/) creates only one RegExp instance,
    even if it occurs inside a function. Not once each time the function
    is called. Since the regexp literal has the "g" flag, the same case
    as above occurs.

    My assumption is the global expression holds an internal status, but
    that doesn't explain the second example.
    It does :)

    /L
    --
    Lasse Reichstein Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Thomas 'PointedEars' Lahn

      #3
      Re: Why does JavaScript Regx behave weirdly?

      Cain wrote:
      [...]
      var regx = /^\d+$/g;
      alert(regx.test ("11") + ": " + regx.test("11") );
      >
      And the output is "true: false".
      [...] when I only test the string once in a function:
      >
      var regx = /^\d+$/g;
      alert(regx.test ("11") + ": ");
      >
      var regx = new RegExp("^\\d+$" , "g");
      alert(regx.test ("11") + ": ");
      >
      If I call the function twice, the output will be
      >
      1st call:
      true, true
      >
      2nd call:
      true, false
      [...]
      Evidently, you have not been paying attention:

      <48D2E953.30308 08@PointedEars. de>
      (Fri, 19 Sep 2008 01:50:43 +0200)

      <672deda4-6678-4abb-b8f4-a5f4d714775f@a7 0g2000hsh.googl egroups.com>
      (Mon, 22 Sep 2008 07:15:23 -0700 (PDT))

      See also <http://www.jibbering.c om/faq/#FAQ2_3>.


      PointedEars
      --
      var bugRiddenCrashP ronePieceOfJunk = (
      navigator.userA gent.indexOf('M SIE 5') != -1
      && navigator.userA gent.indexOf('M ac') != -1
      ) // Plone, register_functi on.js:16

      Comment

      • Cain

        #4
        Re: Why does JavaScript Regx behave weirdly?

        Thank you for the wonderful explanation:)
        I have been struggling on this for so many days, and couldn't find a
        reasonable explanation on the internet.
        Thanks a million~

        Comment

        Working...