Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Empty groups in JavaScript

Last post 04-07-2008, 7:12 PM by Aussie Susan. 5 replies.
Sort Posts: Previous Next
  •  04-03-2008, 8:32 AM 41009

    Empty groups in JavaScript

    I have this regular expression that evaluate differently in JavaScript versus Java:
    var r = new RegExp("^|([0-9][0-9])$", "")

    The intent is to accept an string or a 2-digit string. However the call r.test("abc") returns true !

    In Java, the equivalent code, Pattern.matches("^|([0-9][0-9])$", "abc") returns false.

    It seems that empty groups are not handle correctly in JavaScript. Any hints?

    Filed under:
  •  04-03-2008, 11:26 AM 41023 in reply to 41009

    Re: Empty groups in JavaScript

    Although there is a case of JavaScript handling groups differently, that's not the case here. You expression as defined matches two condition

    1) The begining of the input

    2) a string ending in two digits. (Note: not a two digit string but a string of any length ending in two digits)

    The correct result for your test is always true because case 1 is always true of any string.  The real question is why Java is returning false?

    I think the expression you want is 

    ^(|[0-9][0-9])$

     


    Michael

    "In theory, theory and practice are the same. In practice, they are not."
    Albert Einstein
  •  04-03-2008, 6:18 PM 41047 in reply to 41009

    Re: Empty groups in JavaScript

    agilbert:

    The intent is to accept an string or a 2-digit string. However the call r.test("abc") returns true !

    Can you please clarify the "accept an[sic] string or a 2-digit string"(my emphasis) part of your intent.

    Does this mean that a valid string is any number of characters (hence your test case of "abc") OR just 2 digits.

    If this is the case, then surely a 2 digit string is a complete subset of a string of any characters of any length?

    Susan 

  •  04-07-2008, 1:13 PM 41159 in reply to 41047

    Re: Empty groups in JavaScript

    Sorry for the errors in my statement. Let's state it again: The intent is to accept an empty string or a 2-digit string.

    Alain

  •  04-07-2008, 1:40 PM 41160 in reply to 41023

    Re: Empty groups in JavaScript

    Thank you for pinpointing my error. I was not considering the ^ part of the first alternative. So I rewrite my expression as:

    new RegExp("^(?:|([0-9][0-9]))$", "")

    I'm introducing a non-capturing group to ensure that ^ and $ are not part of the alternatives. I may have 1 or n alternatives. My example had 2.

    Java & JavaScript handle identically this rewritten expression.

    Thanks,
    Alain

  •  04-07-2008, 7:12 PM 41174 in reply to 41160

    Re: Empty groups in JavaScript

    Alain,

    Something to keep in mind when you are creating your alternatives is that they are tested in the order you write them (for most regex engines anyway). Therefore, be careful not to mask one alternative by another. For example:

    \w{3}|\d{2}

    will never find the second alternative because the first one includes all of the digits. Of course this is a forced example, but it is one that is surprisingly easy to make when the alternatives get a bit more complex. A good idea is to order them in the most specific to the least, and preferable have a sub-pattern as early in each alternative that is unique so that the wrong paths are found as soon as possible.

    Susan 

View as RSS news feed in XML