Regexp : invalid quantifier +

Discussion in 'Javascript' started by Une Bévue, Jun 4, 2008.

  1. Une Bévue

    Une Bévue Guest

    I wanted to test userAgent by a Regexp

    the goal :

    true if UA contains "AppleWebKit/528+ " or "AppleWebKit/525.12+ " note
    the final "+"

    false otherwise

    then i wrote :

    var re=new Regexp(" AppleWebKit/[^ ]+\+ ");

    var isWebkitNightly=re.test(navigator.userAgent);

    and i got the following error with Firefox3RC1 :

    invalid quantifier +


    however, testing that way :

    var isWebkitNightly=/ AppleWebKit\/[^ ]+\+ /.test(navigator.userAgent);

    works fine.

    why ?
    --
    Une Bévue
     
    Une Bévue, Jun 4, 2008
    #1
    1. Advertising

  2. Une Bévue wrote:

    > I wanted to test userAgent by a Regexp
    >
    > the goal :
    >
    > true if UA contains  "AppleWebKit/528+ " or "AppleWebKit/525.12+ "
    > note the final "+"
    >
    > false otherwise
    >
    > then i wrote :
    >
    > var re=new Regexp(" AppleWebKit/[^ ]+\+ ");
    >
    > var isWebkitNightly=re.test(navigator.userAgent);
    >
    > and i got the following error with Firefox3RC1 :
    >
    > invalid quantifier +
    >
    > however, testing that way :
    >
    > var isWebkitNightly=/ AppleWebKit\/[^ ]+\+
    > /.test(navigator.userAgent);
    >
    > works fine.
    >
    > why ?


    'new Regexp' should be 'new RegExp', and the last plus should be
    escaped double.

    var re=new RegExp(" AppleWebKit/[^ ]+\\+ ");

    --
    Bart
     
    Bart Van der Donck, Jun 4, 2008
    #2
    1. Advertising

  3. Une Bévue

    Une Bévue Guest

    Bart Van der Donck <> wrote:

    > 'new Regexp' should be 'new RegExp',


    yes, it's a typo of me )))

    > and the last plus should be
    > escaped double.
    >
    > var re=new RegExp(" AppleWebKit/[^ ]+\\+ ");


    i've found that too in the mean time )))

    but if i enter the following (without quotes) :

    " AppleWebKit/[^ ]+\+ " // NOT double escaped

    in an input text field as in this RegExp tester :
    <http://www.regular-expressions.info/javascriptexample.html>

    it works fine, does that means, because it is in an input text, a second
    escape (ie. \) is inserted, behind the scene, the text being supposed to
    be POSted ??? and this second \ isn't seen by an alert(that.input.value)
    ???
    --
    Une Bévue
     
    Une Bévue, Jun 4, 2008
    #3
  4. Une Bévue wrote:

    > Bart Van der Donck <> wrote:
    >
    >> 'new Regexp' should be 'new RegExp',

    >
    > yes, it's a typo of me )))
    >
    >> and the last plus should be
    >> escaped double.

    >
    >>   var re=new RegExp(" AppleWebKit/[^ ]+\\+ ");

    >
    > i've found that too in the mean time )))
    >
    > but if i enter the following (without quotes) :
    >
    > " AppleWebKit/[^ ]+\+ " // NOT double escaped
    >
    > in an input text field as in this RegExp tester :
    > <http://www.regular-expressions.info/javascriptexample.html>
    >
    > it works fine, does that means, because it is in an input text, a second
    > escape (ie. \) is inserted, behind the scene, the text being supposed to
    > be POSted ??? and this second \ isn't seen by an alert(that.input.value)
    > ???


    It hasn't anything to do with the '+'. It is the backslash itself that
    needs to be escaped, so a correct '\+' is received to perform the
    regexp with. When the regexp is taken from an input box, the backslash
    doesn't need to be escaped because it is then passed as the actual
    character by itself.

    --
    Bart
     
    Bart Van der Donck, Jun 4, 2008
    #4
  5. On Jun 4, 12:00 pm, (Une
    Bévue) wrote:
    >
    > it works fine, does that means, because it is in an input text, a second
    > escape (ie. \) is inserted, behind the scene, the text being supposed to
    > be POSted ??? and this second \ isn't seen by an alert(that.input.value)
    > ???


    Got it backwards. The input text only gets parsed for
    escape sequences in the regexp, however when typed in
    as a javascript constant string it get parsed twice,
    first by the javascript parser to create the 'correct'
    string value, then by the regexp object.

    ---
    Geoff
     
    Geoffrey Summerhayes, Jun 4, 2008
    #5
  6. Une Bévue

    Une Bévue Guest

    Geoffrey Summerhayes <> wrote:

    >
    > Got it backwards. The input text only gets parsed for
    > escape sequences in the regexp, however when typed in
    > as a javascript constant string it get parsed twice,
    > first by the javascript parser to create the 'correct'
    > string value, then by the regexp object.


    perfectly clear thanks !
    --
    Une Bévue
     
    Une Bévue, Jun 4, 2008
    #6
  7. Une Bévue

    Une Bévue Guest

    Bart Van der Donck <> wrote:

    >
    > It hasn't anything to do with the '+'. It is the backslash itself that
    > needs to be escaped, so a correct '\+' is received to perform the
    > regexp with. When the regexp is taken from an input box, the backslash
    > doesn't need to be escaped because it is then passed as the actual
    > character by itself.


    ok, clear enough, thanks !

    --
    Une Bévue
     
    Une Bévue, Jun 4, 2008
    #7
    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. -

    Greedy quantifier

    -, Jul 11, 2005, in forum: Java
    Replies:
    0
    Views:
    497
  2. Joao Silva
    Replies:
    16
    Views:
    403
    7stud --
    Aug 21, 2009
  3. Philippe Aymer
    Replies:
    12
    Views:
    235
    Philippe Aymer
    Oct 14, 2004
  4. Replies:
    19
    Views:
    471
    Dr.Ruud
    May 7, 2006
  5. Jack
    Replies:
    2
    Views:
    341
    Tad McClellan
    Oct 4, 2006
Loading...

Share This Page