RegExp.exec() returns null when there is a match - a JavaScript RegExp bug?

Discussion in 'Javascript' started by Uldis Bojars, Dec 17, 2006.

  1. Uldis  Bojars

    Uldis Bojars Guest

    Hi All,

    I have encountered problems with JS RegExp.exec() and can't find what
    is the problem. Could you help me?

    formRequest is a function that extracts some information from
    XMLHTTPRequest response. A very strange effect (and I can't find where
    I've done something wrong) is that regexp matches in this function fail
    on every second call.

    Contents of the 'response' variable are similar in all calls and I
    tested with javascript regexp evaluator that there is a match for these
    regexped in the response text. Nevertheless JS returns null.

    function formRequest( response )
    {

    var re = /<input type=hidden name="([^"]*)" value="([^"]*)">/g;
    var m2 = re.exec( response );
    alert(m2);

    var re = /<input src="([^"]*)" border="([^"]*)"
    height="([^"]*)" type="image" width="([^"]*)" name="([^"]*)"
    value="([^"]*)">/g;
    var m3 = re.exec( response );
    alert(m3);

    }

    A simple workaround is to check if exec() returns null and do the
    request again:
    if (m2 == null)
    m2 = re.exec( response );
    This helps and then the problem shifts to "var m3 = re.exec( response
    );" which can be fixed in a similar manner.

    While this fixes the problem somehow it is not acceptable that a
    program (or JavaScript?) behaves in this seemingly unrational way and
    I'd like to get it working properly.

    P.S. Script is being executed from a XUL extension in Mozilla Firefox
    1.5.

    Thanks,
    Uldis

    [ http://captsolo.net/info/ ]
     
    Uldis Bojars, Dec 17, 2006
    #1
    1. Advertising

  2. Uldis  Bojars

    Lee Guest

    Uldis Bojars said:
    >
    >Hi All,
    >
    >I have encountered problems with JS RegExp.exec() and can't find what
    >is the problem. Could you help me?


    TFM says:

    If the match succeeds, the exec method returns an array and
    updates properties of the regular expression object and the
    predefined regular expression object, RegExp. If the match
    fails, the exec method returns null.

    If your regular expression uses the "g" flag, you can use the
    exec method multiple times to find successive matches in the
    same string. When you do so, the search starts at the substring
    of str specified by the regular expression's lastIndex property.

    http://docs.sun.com/source/816-6408-10/regexp.htm#1194735


    --
     
    Lee, Dec 17, 2006
    #2
    1. Advertising

  3. Uldis Bojars wrote:
    > A simple workaround is to check if exec() returns null and do the
    > request again:
    > if (m2 == null)
    > m2 = re.exec( response );
    > This helps and then the problem shifts to "var m3 = re.exec( response
    > );" which can be fixed in a similar manner.
    >


    The exec() method stores the result indexes in the RegExp.lastIndex property
    and starts searching from that point for future uses. Resetting the property
    to 0 solves your problem.

    Example:

    var what = 'dabcd';

    var re = /a/g;
    var m2 = re.exec(what);
    alert(m2); // alerts a

    var m3 = re.exec(what);
    alert(m3); // alerts null

    re.lastIndex = 0;
    var m3 = re.exec(what);
    alert(m3); // alerts a


    JW
     
    Janwillem Borleffs, Dec 17, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vizlab
    Replies:
    3
    Views:
    4,240
    Michael Bar-Sinai
    Oct 17, 2007
  2. Replies:
    5
    Views:
    27,021
    Mike Schilling
    Mar 29, 2006
  3. Col
    Replies:
    1
    Views:
    639
    peter koch
    Apr 21, 2006
  4. Mikel Lindsaar
    Replies:
    0
    Views:
    512
    Mikel Lindsaar
    Mar 31, 2008
  5. Old Echo
    Replies:
    1
    Views:
    190
    Adam Shelly
    Sep 4, 2008
Loading...

Share This Page