JavaScript RegExp Quantifiers

Discussion in 'ASP .Net' started by Nathan Sokalski, Jun 11, 2008.

  1. I have the following script that I am using to test some JavaScript RegExp
    code:

    function RE()
    {
    var testing1=new RegExp("[.]*");
    var testing2=new RegExp("[.]{0,}");
    var testing3=new RegExp("[.]+");
    var testing4=new RegExp("[.]{1,}");
    window.alert(testing1.test("ab")+"\n"+testing2.test("ab")+"\n"+testing3.test("ab")+"\n"+testing4.test("ab"));}When this script is run, the window.alert contains the following results:truetruefalsefalseThe the first two trues make since, but why are the falses false? Thequantifiers in the RegExp, if I understand correctly, say it should beallowed to have 1 or more characters (other than newlines and lineterminators), and I know that 'a' and 'b' are not newlines or lineterminators, and "ab" is more than 1 character. I am using Internet Explorer6.0. Is this a bug in IE? Am I doing something else wrong? Any help would beappreciated. Thanks.--Nathan ://www.nathansokalski.com/
    Nathan Sokalski, Jun 11, 2008
    #1
    1. Advertising

  2. Nathan Sokalski wrote:
    > I have the following script that I am using to test some JavaScript RegExp
    > code:
    >
    > function RE()
    > {
    > var testing1=new RegExp("[.]*");
    > var testing2=new RegExp("[.]{0,}");
    > var testing3=new RegExp("[.]+");
    > var testing4=new RegExp("[.]{1,}");
    > window.alert(testing1.test("ab")+"\n"+testing2.test("ab")+"\n"+testing3.test("ab")+"\n"+testing4.test("ab"));}When this script is run, the window.alert contains the following results:truetruefalsefalseThe the first two trues make since, but why are the falses false? Thequantifiers in the RegExp, if I understand correctly, say it should beallowed to have 1 or more characters (other than newlines and lineterminators), and I know that 'a' and 'b' are not newlines or lineterminators, and "ab" is more than 1 character. I am using Internet Explorer6.0. Is this a bug in IE? Am I doing something else wrong? Any help would beappreciated. Thanks.--Nathan ://www.nathansokalski.com/
    >


    Inside a set, the period doesn't mean "any character except newline", it
    just means a period.

    testing3.test("ab.") will return true, as there is a period in the string.


    The testing1 and testing2 patterns are equivalent, and also equivalent
    with the pattern "\.*".

    The testing3 and testing4 patterns are equivalent, and also equivalent
    with the pattern "\.+".

    --
    Göran Andersson
    _____
    http://www.guffa.com
    Göran Andersson, Jun 11, 2008
    #2
    1. Advertising

  3. Nathan Sokalski

    Hans Kesting Guest

    Göran Andersson wrote on 11-6-2008 :
    > Nathan Sokalski wrote:
    >> I have the following script that I am using to test some JavaScript RegExp
    >> code:
    >>
    >> function RE()
    >> {
    >> var testing1=new RegExp("[.]*");
    >> var testing2=new RegExp("[.]{0,}");
    >> var testing3=new RegExp("[.]+");
    >> var testing4=new RegExp("[.]{1,}");
    >>
    >> window.alert(testing1.test("ab")+"\n"+testing2.test("ab")+"\n"+testing3.test("ab")+"\n"+testing4.test("ab"));}When
    >> this script is run, the window.alert contains the following
    >> results:truetruefalsefalseThe the first two trues make since, but why are
    >> the falses false? Thequantifiers in the RegExp, if I understand correctly,
    >> say it should beallowed to have 1 or more characters (other than newlines
    >> and lineterminators), and I know that 'a' and 'b' are not newlines or
    >> lineterminators, and "ab" is more than 1 character. I am using Internet
    >> Explorer6.0. Is this a bug in IE? Am I doing something else wrong? Any help
    >> would beappreciated. Thanks.--Nathan
    >> ://www.nathansokalski.com/
    >>

    >
    > Inside a set, the period doesn't mean "any character except newline", it just
    > means a period.
    >
    > testing3.test("ab.") will return true, as there is a period in the string.
    >
    >
    > The testing1 and testing2 patterns are equivalent, and also equivalent with
    > the pattern "\.*".
    >
    > The testing3 and testing4 patterns are equivalent, and also equivalent with
    > the pattern "\.+".


    And to add to this:
    [.]* means: a period, *zero* or more times. And "ab" contains zero
    periods.
    [.]+ means: at least *one* period, so "ab" does not match.

    Hans Kesting
    Hans Kesting, Jun 13, 2008
    #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. Replies:
    1
    Views:
    371
    Joshua Cranmer
    Sep 9, 2007
  2. NeoGeoSNK
    Replies:
    4
    Views:
    3,653
    Joshua Cranmer
    Mar 3, 2008
  3. Johnathan Smith

    rexular expression quantifiers

    Johnathan Smith, Jan 7, 2008, in forum: Ruby
    Replies:
    4
    Views:
    105
    Giles Bowkett
    Jan 7, 2008
  4. Joao Silva
    Replies:
    16
    Views:
    337
    7stud --
    Aug 21, 2009
  5. Replies:
    0
    Views:
    130
Loading...

Share This Page