Inconsistent regex results

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

    Inconsistent regex results

    Hi,

    I wanted to check if this was correct be behavior before reporting it as a
    bug in FireFox.

    Given this function:

    function test()
    {
    var re;

    re = /^(.+)\@(.+?)$/gi;
    if (re.exec("test@ example.com") != null)
    {
    alert("Match");
    } else {
    alert("Null");
    }

    return;
    }

    Should it alternately between displaying "Match" and "Null" each time it's
    called?

    I would think that since the regex and the string are always the same if
    should always display "Match"???

    What I am seeing is that it alternates.

    If this is proper behavior, why?

    Gary


  • Martin Honnen

    #2
    Re: Inconsistent regex results

    Gary Wardell wrote:
    Should it alternately between displaying "Match" and "Null" each time it's
    called?
    >
    I would think that since the regex and the string are always the same if
    should always display "Match"???
    >
    What I am seeing is that it alternates.
    >
    If this is proper behavior, why?
    Use

    function test()
    {
    var re;

    re = new RegExp('^(.+)\\ @(.+?)$', 'gi');
    if (re.exec("test@ example.com") != null)
    {
    alert("Match");
    } else {
    alert("Null");
    }

    return;
    }

    or live with the ECMAScript specification insisting on creating one
    regular expression object for each regular expression literal, even if
    it is inside of a function as a local variable:
    "7.8.5 Regular Expression Literals
    A regular expression literal is an input element that is converted to a
    RegExp object (section 15.10) when it is
    scanned. The object is created before evaluation of the containing
    program or function begins. Evaluation of the
    literal produces a reference to that object; it does not create a new
    object."

    So what happens is that in your code the regular expression literal
    creates one regular expression object with the g flag when the program
    code is scanned, and then when the function is executed that regular
    expression object is referenced and because of the g flag every second
    exec starts after the last match and does not find anything.

    --

    Martin Honnen

    Comment

    • Bjoern Hoehrmann

      #3
      Re: Inconsistent regex results

      * Gary Wardell wrote in comp.lang.javas cript:
      >I wanted to check if this was correct be behavior before reporting it as a
      >bug in FireFox.
      >
      >Given this function:
      >
      >function test()
      >{
      var re;
      >
      re = /^(.+)\@(.+?)$/gi;
      if (re.exec("test@ example.com") != null)
      {
      alert("Match");
      } else {
      alert("Null");
      }
      >
      return;
      >}
      >
      >Should it alternately between displaying "Match" and "Null" each time it's
      >called?
      A quick reading of ECMA-262 15.10.6.2 suggests just that, yes. Note that
      you are using the 'global' ('g') property so; if you don't, you get the
      result you expect.
      --
      Björn Höhrmann · mailto:bjoern@h oehrmann.de · http://bjoern.hoehrmann.de
      Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
      68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/

      Comment

      • Evertjan.

        #4
        Re: Inconsistent regex results

        Gary Wardell wrote on 24 apr 2008 in comp.lang.javas cript:
        Hi,
        >
        I wanted to check if this was correct be behavior before reporting it
        as a bug in FireFox.
        >
        Given this function:
        >
        function test()
        {
        var re;
        >
        re = /^(.+)\@(.+?)$/gi;
        if (re.exec("test@ example.com") != null)
        {
        alert("Match");
        } else {
        alert("Null");
        }
        >
        return;
        >}
        >
        Should it alternately between displaying "Match" and "Null" each time
        it's called?
        >
        I would think that since the regex and the string are always the same
        if should always display "Match"???
        >
        What I am seeing is that it alternates.
        >
        If this is proper behavior, why?
        1 You should use .test() to test, that's where test() is for.

        2 in exec() the ponter of the defined regex object is kept from the last
        call, this could be your problem.
        re = /^(.+)\@(.+?)$/gi;
        3 @ does not need to be escaped,
        the () are unneccessary,
        the g flag is not used,
        the i flag is not used.

        Try:

        if ( /^.+@.+$/.test('test@exa mple.com') )
        alert('Match')
        else
        alert('No match');




        --
        Evertjan.
        The Netherlands.
        (Please change the x'es to dots in my emailaddress)

        Comment

        • Gary Wardell

          #5
          Re: Inconsistent regex results

          Hi Martin,

          Thanks for the explanation.

          Given the regex ^(.+)\@(.+?)$ I'm wondering if a really need the "gi",
          since it's matching the whole string and it doesn't really care about case?

          Is "new" the preferred way to create regex objects or is it just better in
          this case?

          I'm a little bit new to both regex and JavaScript.

          Gary


          Comment

          • Martin Honnen

            #6
            Re: Inconsistent regex results

            Gary Wardell wrote:
            Given the regex ^(.+)\@(.+?)$ I'm wondering if a really need the "gi",
            since it's matching the whole string and it doesn't really care about case?
            >
            Is "new" the preferred way to create regex objects or is it just better in
            this case?
            No, you can use regular expression literals but you need to be aware of
            the consequences. In your sample you could drop the g flag and also, as
            already suggested, use the test method instead of the exec method.


            --

            Martin Honnen

            Comment

            • Gary Wardell

              #7
              Re: Inconsistent regex results

              Hi,

              Yes, I came across that in looking for why it wasn't working.

              However, exec returns the captured strings in an array and I need to have
              the account separated from the domain name.

              I left that out to simplify the sample code.

              It's working now.

              Thanks,

              Gary


              Comment

              Working...